LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   strings in c (https://www.linuxquestions.org/questions/programming-9/strings-in-c-276120/)

djgerbavore 01-10-2005 11:20 AM

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;
and to fill up array wiht data to do:
Code:

*string[0] = "foo";
*string[1] = "oof";

but the compiler (gcc) is complaining. I'm little confused on how array of pointers work. Any help would be apprecite.


thanks,

djgerbvore

exvor 01-10-2005 11:39 AM

Like this

Code:

char *array[]=
{
  "foo",
  "oof",
};


exvor 01-10-2005 11:47 AM

Quote:

second, how to the traverse through the whole array?

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.

djgerbavore 01-10-2005 04:24 PM

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]);
will print out bar!?!

thanks,


djgerbavore,

sorry for the newbie questions, strings are little confusing in c.

Hko 01-11-2005 07:06 AM

Quote:

Originally posted by djgerbavore
if i have an array of strings, how do i access indivual strings: for example

array = "foo", "bar", "bas"
The comma-operator doesn't work like that. All after the first comma do not have any effect.

Try this:
Code:

#include <stdio.h>

int main()
{
    int i;
    char *array[] = { "one", "two", "three" };

    for (i = 0; i < 3; ++i) {
        printf("%s ", array[i]);
    }
    printf("\n");

    return 0;
}


djgerbavore 01-11-2005 02:56 PM

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

Hko 01-11-2005 03:34 PM

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?

itsme86 01-11-2005 04:20 PM

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>
#include <stdlib.h>

void print_strs(char **strs)
{
  int i;

  for(i = 0;i < 3;++i)
    puts(strs[i]);
}

int main(void)
{
  char **strs;

  strs = malloc(sizeof(char *) * 3);  // We need to allocate space!

  strs[0] = "first";
  strs[1] = "second";
  strs[2] = "third";

  print_strs(strs);

  free(strs);

  return 0;
}

Code:

itsme@dreams:~/C$ ./strings
first
second
third

People tend to misunderstand the statement "array notation degrades to pointer notation" as "arrays and pointers are the same thing". They're not. You can use array notation and pointer notation interchangably most of the time, but during initializing? no. You also can't increment an array but you can a pointer.
Code:

{
  char array[1];
  char *fakearray = malloc(1);

  array++;  // BZZZZT! Compiler error
  fakearray++;  // Works fine
}


djgerbavore 01-11-2005 04:27 PM

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.