strings in c
i'm writing programs in c and i need help on strings in c.
first, how do i declare a array of strings? second, how to the traverse through the whole array? third on a good site on using array of pointers (String array). i thought to declare string you just Code:
char **string; Code:
*string[0] = "foo"; thanks, djgerbvore |
Like this
Code:
char *array[]= |
Quote:
you mean like obtain the data in them ? the data would be array[0] = foo array [1] = oof the above is not literal code you couldent access the data this way but just shows where in the arrray of strings the data is array[0][0] for example contains the data for char f rember that data in a string is just numerical data of charecters. I got this info from the gnu c tutorial any google search will lead where you can get this info. Needless to say this is used mostly for string blocks of data to be printed all at once. For clarity i would say use a structure instead of a array of strings. But thats my own opinon using a structure would probably make it eazer to change values in the array as well. |
thanks guys,
if i have an array of strings, how do i access indivual strings: for example array = "foo", "bar", "bas" can i just deference my array by saying Code:
printf("array[1] = %s\n", array[1]); thanks, djgerbavore, sorry for the newbie questions, strings are little confusing in c. |
Quote:
Try this: Code:
#include <stdio.h> |
hey thanks everyone for you help. I alot clearer on strings and array in c. I just have one more queston.
Code:
char **strptr; Code:
char *strarr[]; what is the difference between them both. I've try to use the first statement with no luck, but to my understanding the two statements should be pretty much the same thing? How are they different, and how do you use the first statement and how do i know which one to use? thanks djgerbavore |
This explanation is incomplete, and maybe not 100% correct:
You cannot assign constant (fixed, hardcoded) strings to char **array, but you can assign them to a char *array[]. I don't anymore about this, and I'm curious as well. There is some difference in the way they are stored in memory, but I don't know why/how. Somebody knows more about this? |
The main difference is that an array will automatically allocate the needed space for the pointers. The process is the same as something like int array[] = { 1, 2, 3 };. It allocates memory for 3 integers on the stack. A char ** pointer, however, gets sizeof(any_pointer) and that's it. You can't initialize a char ** the same was as an array because it's just simply not an array. A char ** can be used as an array in some cases like in the following example:
Code:
#include <stdio.h> Code:
itsme@dreams:~/C$ ./strings Code:
{ |
itsme86 - your reply was beautifully put. You have no idea how long and hard i was longing for an answer like that. Ever website (even gnu c tutorial) wasn't clear on the difference of arrays and pointers. The fog has been lifted, everyone needs to watch out now, because i'm back in business :)
thanks so much djgerbavore |
All times are GMT -5. The time now is 01:24 AM. |