Declaring string arrays

Hi everyone,

I'm just wondering why this

char *heading[5];
char *text[5];

works. (they're declared as globals)

I assign strings to the elements later in a method and then print them out on an LCD and it all works fine.
It just bothers me that it does, because as far as I've read, you usually have to define the size of an array. But I haven't done this for the string dimension and it still works.

Best regards,
thox

Why do you think that wouldn't "work" ( whatever "work" means in this snippet context)?
What has your question got to do with the topic title?

I assign strings to the elements later in a method

No, you make the pointers point to existing strings some place else. That is not the same thing.

You're a little mixed up.

//I want memory to store a 4 character string.
char some[4]; 

//I want memory to store a memory address that points to an undefined number of chars.
char *some; 

//I want enough memory to store 4 addresses that point to an undefined number of chars each.
char *some[4];

As PaulS said, the compiler is smart enough to handle this for you, but what's happening when assigning to char *some [ x ], is that you ask for memory, copy the chars into it, and then assign that value into char * [ x ] so it points to the right place. You could do it by hand too

WARNING: rusty programmer's code ahead so take it with a rather big grain of salt :smiley:

char *some[4]; //Got 4 pointers to char

some[1] = "hello";

is the same as

some[1] = malloc(6 * sizeof(char));
some[1][0] = 'H';
some[1][1] = 'e';
some[1][2] = 'l';
some[1][3] = 'l';
some[1][4] = 'o';
some[1][5] = '\0';

and when done with the string, you better remember freeing the memory yourself or you'll be leaking.

Edit: I hate this forum software. :frowning:

is the same as

Nope.
The "hello" remains in the same place, no memory is allocated and so no memory needs to be freed.

Okay, thanks a lot.
Yes, "mixed up" describes it well, I guess :smiley:
I got confused by the pointer/array concept. To me it looked like the code generated a 2-dimensional array with one dimension being free, the latter of which shouldn't be possible in that way. That's what I meant by "works". And that 2-dimensional array I thought of was what I meant by "string array", so I hope that explains the title.

So, just to make sure that I got this right:
What I did was declaring a one-dimensional array of pointers, and when I later assigned something like

heading[0] = "somestring";

I actually just placed that string somewhere and told heading[0] to point at it. Is that correct?
If it is, does the memory containing "somestring" get freed when heading[0] gets a new string to point at?

And just to understand the workings of this: If I were to declare an actual 2-dimensional string array, I would use

char heading[3][5];

which would generate an array with 3 pointers each pointing at an array of 5 chars. Hope that's right.

Thanks again!

Oops, sorry for the late reply...

thox:
So, just to make sure that I got this right:
What I did was declaring a one-dimensional array of pointers, and when I later assigned something like

heading[0] = "somestring";

I actually just placed that string somewhere and told heading[0] to point at it. Is that correct?
If it is, does the memory containing "somestring" get freed when heading[0] gets a new string to point at?

Correct. My knowledge of how C handles strings internally is not the best :), but based on how other languages do it (there are several techniques for this), yes, whatever the code is doing, your memory is managed automatically: if it was allocated automatically, it will be freed automatically.

thox:
And just to understand the workings of this: If I were to declare an actual 2-dimensional string array, I would use

char heading[3][5];

which would generate an array with 3 pointers each pointing at an array of 5 chars. Hope that's right.

Thanks again!

Nope, you would create an array of arrays, the memory for it being allocated immediately, so you are using 35 = 15 bytes for it, whether you put something in it or not. That's precisely the idea behind using pointers instead of arrays[].

if it was allocated automatically, it will be freed automatically.

The compiler reserves the memory and the runtime will create SRAM static allocated variables of finite length which is why a null marks the end of the legitimate characters used.

Private variables in functions are allocated dynamically on the stack.

Strings (big S) allocated w/o the .reserve declaration are dynamic heap allocated vars.

This short explanation is simply written.

Ray

We've done this before, but figuring out what is actually being defined is actually pretty simple when you use the Right-Left Rule. One of the more interesting examples is:

double (*(*pf)())[3][4];

Richard Buckland, prof U. New South Wales, has some YouTube videos which cover a variety of C/C++ topics and are rather fun. Reference page.

Specific, a pointer overview is here.

Ray