DPRG
DPRG List  



[DPRG] ANSI C question

Subject: [DPRG] ANSI C question
From: Ed Okerson eokerson at texasconnect.net
Date: Fri Oct 29 20:39:56 CDT 2004

Rick,

Both of the examples you gave should evaluate to the exact same thing, a
two dimensional array of char pointers, those pointers are pointing to
constant character strings.  The only difference in the example is when
the pointer is assigned, at the creation time or later.  With some
compilers this will affect where the space for the pointers is allocated.
Neither implementation is inherently leaky, it just depends on how later
code uses them.  If the code will later re-assign the pointers to point to
other strings, then you have to be careful to either point them to other
constant strings, or properly allocate space for them with malloc() or
calloc() if strings are going to be built dynamically with strcat(),
sprintf() or the like.  If the contents of the pointers is never going to
change then use the second version, but declare it as:

 const char *text[2][5] =
 	{
 	"abcd",
 	"efgh",
 	"ijkl",
 	"mnop",
 	"qrst"
 	}
 	{
 	"abcd",
 	"efgh",
	"ijkl",
 	"mnop",
 	"qrst"	}
 	};

The const modifier will prevent future operations from assigning new
values to the pointers.  Note also that this does not limit the length of
the constant strings in the initializer.

Ed Okerson

On Fri, 29 Oct 2004, Rick J. Bickle wrote:

> Chuck,
>
> Yes, although you would need text[3][5] in your example.
>
> If I declare the array as originally stated:
>
> Char *text [2][5];
>
> text[0][0] = "Msg 0,0   ";
> text[0][1] = "Msg 0,1   ";
> text[0][2] = "Msg 0,2   ";
> text[0][3] = "Msg 0,3   ";
> text[0][4] = "Msg 0,4   ";
> text[0][5] = "Msg 0,5   ";
>
> text[1][0] = "Msg 1,0   ";
> text[1][1] = "Msg 1,1   ";
> text[1][2] = "Msg 1,2   ";
> text[1][3] = "Msg 1,3   ";
> text[1][4] = "Msg 1,4   ";
> text[1][5] = "Msg 1,5   ";
>
> Is it not true that the compiler will only allocate memory space for
> 5x2=10 pointers instead of 10 strings?
> With this declaration, how is the compiler to know the length of each
> string?
> I suspect that this will leak memory, although the compiler generates no
> warnings or errors.
>
> Alternatively, if I declare the array as:
>
> Char *text [2][5] = {
> 	{
> 	"abcd",
> 	"efgh",
> 	"ijkl",
> 	"mnop",
> 	"qrst"
> 	}
> 	{
> 	"abcd",
> 	"efgh",
> 	"ijkl",
> 	"mnop",
> 	"qrst"	}
> 	};
>
> Then the compiler should be able to determine the size of each string
> and allocate memory correctly. However, are we creating an array of
> pointers to strings, or an array of strings themselves?
>
> In a stack of 5 C books, I cannot find a satisfactory answer.
>
> Thanks,
> Rick
>
>
> -----Original Message-----
> From: dprglist-bounces at dprg.org [mailto:dprglist-bounces at dprg.org] On
> Behalf Of Chuck McManis
> Sent: Friday, October 29, 2004 3:53 PM
> To: Rick J. Bickle; dprglist at dprg.org
> Subject: Re: [DPRG] ANSI C question
>
>
> It kind of depends on what you want. Dale's comments are good but don't
> point out that you're declaring a two dimensional array of pointers, to
> 6
> byte entities. (5 chars + nul).
>
> You could also declare it thusly :
> char *text [2][5] = {
>          { "Msg 0,0", "Msg 0,1", "Msg 0,2", "Msg 0,3", "Msg 0,4" },
>          { "Msg 1,0", "Msg 1,1", "Msg 1,2", "Msg 1,3", "Msg 1,4" } };
>
> Note that I removed the space at the end, but you could put it back.
>
> Now in your question you said :
>
> >What is the correct syntax to declare a multidimensional array of fixed
> >length strings?
>
> And a declaration of
> char text [2][5];
>
> Would do that (note that there is no "*" on the text!!  You would
> initialize it thusly :
>
> char text[2][5] = {
>          "Msg0",
>          "Msg1",
>          "Msg2"
> };
>
> That declares 3 (0,1,2) fixed length character arrays of 5 characters
> each.
> As you see I've shortened the message still further to be 4 characters +
>
> nul (5 total)
>
> --Chuck
>
>
> At 11:46 AM 10/29/2004, Rick J. Bickle wrote:
>
> >Hey all you C gurus:
> >
> >I'm working on a programming project and have a question.
> >What is the correct syntax to declare a multidimensional array of fixed
> >length strings?
> >
> >If you declare an array of char pointers like:
> >
> >Char *text [2][5];
> >
> >text[0][0] = "Msg 0,0   ";
> >text[0][1] = "Msg 0,1   ";
> >text[0][2] = "Msg 0,2   ";
> >text[0][3] = "Msg 0,3   ";
> >text[0][4] = "Msg 0,4   ";
> >text[0][5] = "Msg 0,5   ";
> >
> >text[1][0] = "Msg 1,0   ";
> >text[1][1] = "Msg 1,1   ";
> >text[1][2] = "Msg 1,2   ";
> >text[1][3] = "Msg 1,3   ";
> >text[1][4] = "Msg 1,4   ";
> >text[1][5] = "Msg 1,5   ";
> >
> >Will this leak memory? There is no declaration of the size of each
> >string.
> >
> >If this is not right, what is the correct syntax?
> >
> >Rick
> >_______________________________________________
> >DPRGlist mailing list
> >DPRGlist at dprg.org http://list.dprg.org/mailman/listinfo/dprglist
>
>
>
> _______________________________________________
> DPRGlist mailing list
> DPRGlist at dprg.org
> http://list.dprg.org/mailman/listinfo/dprglist
>
>
> _______________________________________________
> DPRGlist mailing list
> DPRGlist at dprg.org
> http://list.dprg.org/mailman/listinfo/dprglist
>


More information about the DPRG mailing list