![]() |
Question about multidimensional arrays of strings
Hi,
I'd just like to know if the following two declarations to create an array of strings are equivalent to each other. :
int main(void)From my understanding, strings1 is an array of 5 pointers-to-char and strings2 is an array of 5 arrays of 21 char elements. In both cases, I see that they can both hold strings but strings2 can only hold a string with a max size of 20 characters whereas strings1 can hold variable length strings using dynamic memory allocation. I guess the better question is, can strings2 be equivalently expressed as strings1 and vice-versa? Sorry if this sounds like such a noob question but my understanding on creating arrays of strings is kind of fuzzy. |
>I guess the better question is, can strings2 be equivalently expressed as strings1 and vice-versa?
This is a subtle area of the language, but yes, strings2 can be equivalently expressed as strings1 and vice-versa. |
This is another form of the old question about equivalence of pointers and arrays.
Although the language treats pointers and arrays as equivalent in some contexts (i.e. one can be used where the other one can) they are not exactly the same thing. In the example; char *strings1[5]; declares an array of five pointers. Those pointers are uninitialised, and it is necessary to make them point at something valid before they can be used. char strings2[5][21]; is a two dimensional array of characters. strings2[i] (for i in the range [0, 4]) is a one-dimensional array of 21 characters. strings2[i], when used in an expression, is also a char * pointer. The following expands on the example, with a few possible usages and comments about what happens; :
#include <string.h> |
If both are essentially equivalent. Isn't there still a limitation on strings2[5][21] where each one dimensional array should only be 20 characters long since the max element is 21? However *strings1[5] imposes no limitation on the length of the string since it's a point to another memory location.
Also is the following code correct? [code] int main(void) { char *strings1[5]; char strings2[5][21]; strings1[0] = "Hello"; //this will assign the beginning address of the Hello string to the first element? strings2[0] = "Hello"; //invalid because strings2[0] is an array? } |
Quote:
To understand what I mean with the underlined bit, consider the "strcpy(strings2[3], "Hello");" from my previous post. The line "strcpy(strings2[3], "Hello");" actually passes two pointers to strcpy. Now, what does strcpy() do? Two (notional) implementations are; :
char *strcpy(char *x, const char *y):
char *strcpy(char *x, const char *y)A hybrid version (which is also equivalent) is; :
char *strcpy(char *x, const char *y)Quote:
strings2[i] is, according to your declaration, an array of char. "Hello" is also a (const) array of char. strcpy() accepts a pointer to char as first argument, and a const pointer to char as second argument. When you use strings2[i] in code after the declaration (eg in my previous example, a call to strcpy) those arrays may be implicitly manipulated as if they are pointers. Quote:
Quote:
Things get more interesting if you subsequently try to manipulate strings1[0], as "Hello" is implicitly const (i.e. it should not be modified). As an exercise, try to explain why this code yields undefined behaviour; :
int main() |
:
char array[21];Just to point out that if you declare an array like above, the array has 21 elements, not 20. From 0-20, including the 0 are all elements. Sorry if Grumpy already mentioned that. |
Quote:
Thanks for your time Grumpy. In your last example, strings[0] stores a pointer to "Hello" first. But I guess it's also a constant string (still not sure what the rules governing this are) which means strings[0] is not modifiable after that point? |
Quote:
So; :
#include <stdlib.h> /* so we can use malloc() */ |
Alright, thanks for your help guys! Greatly appreciated.
|
| All times are GMT -5. The time now is 2:07 AM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC