LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-10-2005, 11:20 AM   #1
djgerbavore
Member
 
Registered: Jun 2004
Location: PA
Distribution: Fedora (latest git kernel)
Posts: 458

Rep: Reputation: 30
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
 
Old 01-10-2005, 11:39 AM   #2
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Rep: Reputation: 87
Like this

Code:
char *array[]= 
{
   "foo",
   "oof",
};
 
Old 01-10-2005, 11:47 AM   #3
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Rep: Reputation: 87
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.

Last edited by exvor; 01-10-2005 at 11:51 AM.
 
Old 01-10-2005, 04:24 PM   #4
djgerbavore
Member
 
Registered: Jun 2004
Location: PA
Distribution: Fedora (latest git kernel)
Posts: 458

Original Poster
Rep: Reputation: 30
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.
 
Old 01-11-2005, 07:06 AM   #5
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
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;
}
 
Old 01-11-2005, 02:56 PM   #6
djgerbavore
Member
 
Registered: Jun 2004
Location: PA
Distribution: Fedora (latest git kernel)
Posts: 458

Original Poster
Rep: Reputation: 30
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
 
Old 01-11-2005, 03:34 PM   #7
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
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?
 
Old 01-11-2005, 04:20 PM   #8
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
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
}

Last edited by itsme86; 01-11-2005 at 04:25 PM.
 
Old 01-11-2005, 04:27 PM   #9
djgerbavore
Member
 
Registered: Jun 2004
Location: PA
Distribution: Fedora (latest git kernel)
Posts: 458

Original Poster
Rep: Reputation: 30
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

Last edited by djgerbavore; 01-11-2005 at 04:28 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to find duplicate strings in vertical column of strings markhod Programming 7 11-02-2005 04:04 AM
C programming strings exvor Programming 8 07-18-2005 04:40 PM
strings question sibtay Programming 7 10-01-2004 10:37 AM
bash and strings graziano1968 Linux - Software 2 10-01-2004 06:50 AM
Storing Strings ToothlessRebel Programming 4 09-05-2004 03:16 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:02 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration