Jump to content

Multi-dimensional string arrays

- - - - -

  • Please log in to reply
4 replies to this topic

#1
AdvMutant

AdvMutant

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 438 posts
Hey.
Recently, on my way back home from school, I was thinking general thoughts about programming. One thing I was wondering is how to create a string array in C. Since a strings in C look like the following -

Quote

char *str1;//Pointer
char str2[];//Pointer
char str3[256];//Array
The solution came up naturally - a two dimensional array.

Quote

char *strArray[5];//5 strings in an array
I managed it to figure out how to use such arrays, but it's still confusing me - How do multi-dimensional arrays work? If I write "char [50] [2]", does the compiler declare 100 characters? Because the array arithmetic doesn't apply here. If I write the following code -

Quote

char *str[5];//5 strings in an array
char *p_str, *p_str2;

str[0] = "This is string number 1.";
str[1] = "This is string number 2.";

p_str = str[0]; p_str2 = str[0] + strlen(str[0]) + 1;
puts(p_str);
puts(++p_str);
puts(p_str2);
This is the output -

Quote

This is string number 1.
his is string number 1.
This is string number 2.
Why does the program output "his is string number 1." instead of "This is string number 2."?

Posted Image
There is no problem that cannot be solved by the use of high explosives.


#2
Ancient Dragon

Ancient Dragon

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 400 posts
char* str[5] declares 5 pointers, not 5 strings. Each of those pointers can be set to point to random memory locations which may or may not be contiguous. In the example you posted, the two strings are not contiguous, so its not possible to get to the second string from the memory address of the first. The only reason that seemed to work in your example is by pure chance, not because it will always work like that.

puts(++p_str) skipped the first character because that's what you told it to do -- increment the pointer then display the contents of the string from that point to the end of the string.

>>If I write "char [50] [2]", does the compiler declare 100 characters?
Yes, but if you intend to have 2 strings with room for 50 character each you would declare it as char str[2][50];
Visit Grandpa's Forums, a social networking forum, with family-oriented arcade games, blogs, discussion forums, and photo albums.

#3
AdvMutant

AdvMutant

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 438 posts
Now I'm even more confused. Yes, char *str[5] declares 5 pointers.
If I haven't initialized the pointers to point a variable, and gave them a value, they point to a random location?
I forgot that pointers aren't like variables, so an array of pointers can still be contiguous. But what if I want them to? Should I use the malloc() function?

Posted Image
There is no problem that cannot be solved by the use of high explosives.


#4
Ancient Dragon

Ancient Dragon

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 400 posts
In an array of pointers, the pointers themself are in contiguous memory but not the items they point to. The only way to insure that a 2d array of strings are in contiguous memory is to declare them like this: char sarray[5][100]; which declares an array of 5 strings, each can hold a maximum of 100 characters including the NULL terminator.
Visit Grandpa's Forums, a social networking forum, with family-oriented arcade games, blogs, discussion forums, and photo albums.

#5
AdvMutant

AdvMutant

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 438 posts
So, the only way I can make sure that the items pointer arrays point to are contiguous, is pointing them to an array?

Posted Image
There is no problem that cannot be solved by the use of high explosives.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users