array of strings

This is a discussion on array of strings within the C Programming forums, part of the General Programming Boards category; Hi, In java you can create an array of strings. For ex: string[10], which can store 10 array of characters. ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    75

    array of strings

    Hi,

    In java you can create an array of strings.
    For ex: string[10], which can store 10 array of characters.
    how do you do this in C?

    is it char str[x][y];

    and then how do you assign a string to the array?
    when i write this: str[0][0]= "some string";
    str[1][0] = "another string";
    i get something bizzare.


    Tanks you.

  2. #2
    Registered User
    Join Date
    Aug 2008
    Location
    Belgrade, Serbia
    Posts
    163
    You use strcpy().
    Code:
    char str[10][100]; //A string array that can hold 10 strings of 100 characters
    
    int main(void)
    {
        strcpy(str[0],"Hello ");
        strcpy(str[0],"World!");
    
        return 0;
    }
    Vanity of vanities, saith the Preacher, vanity of vanities; all is vanity.
    What profit hath a man of all his labour which he taketh under the sun?
    All the rivers run into the sea; yet the sea is not full; unto the place from whence the rivers come, thither they return again.
    For in much wisdom is much grief: and he that increaseth knowledge increaseth sorrow.

  3. #3
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    In C you would have to have a maximum size for a string. So for 10 stings indeed you would do char str[10][MAXSIZE]
    You use functions (strncpy, strcpy) and NEVER the = operator. So you would do strcpy(str[2], "Hey") for the Java str[2] = "Hey"
    In C++ there is a string type by the way. There are other ways around if you don't want a maximum size. That is using strlen() function and malloc() to allocate space for a char* str[10]

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main() {
    	char string[]="hello world";	// a single string
    	char *list[3];			// an array of three strings
    	list[0]=malloc(12);
    	strcpy(list[0],"hello world");
    	list[1]=malloc(6);
    	strcpy(list[1],"world");
    	list[2]=malloc(6);
    	strcpy(list[2],"hello");
    	printf("%s\n%s %s\n%s\n",list[0],list[2],list[1],string);
    }
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,593
    Tsk, MK27, you forgot to free() what you malloc()ed.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Why bbebfe is not bbebfe? bbebfe's Avatar
    Join Date
    Nov 2008
    Location
    Earth
    Posts
    27
    Unlike Java automatic garbage collection mechanism, you need to free the memory allocated by malloc manually in C.

    char *p = (char*)malloc(100);
    strcpy(p, "hello");
    free(p); //free the memory
    Do you know why bbebfe is NOT bbebfe?

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by laserlight View Post
    Tsk, MK27, you forgot to free() what you malloc()ed.
    Yes, and I didn't error check my malloc calls either!

    Code:
    if ((list[0]=malloc(12)) == NULL) perror("for list[0]: ");           // for example
    [...]
    free(list[0]);
    free(list[1]);
    free(list[2]);
    return 0;             // musn't forget!
    }      // end of main()
    Last edited by MK27; 11-19-2008 at 07:38 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  8. #8
    Registered User
    Join Date
    Oct 2008
    Posts
    75
    ok thanks.

    now i have to use the string in execvp.

    Code:
    char str[10][100]; //A string array that can hold 10 strings of 100 characters
    
        strcpy(str[0],"ls");
        strcpy(str[1],"dir");
    fork()...
    ...
    
    execvp(str[0], str)
    but it's not working.

    any idea why?


    Thanks.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Describe "not working".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    The trick with execvp is twofold:

    1st) the first argument is a string which is really a path to the program. Since execvp checks the PATH env. variable, you can just use the command name ("ls"). The
    2nd argument is a list, but the first element of the list must be the file/command name (without a path), so in this case it will be the same thing ("ls"). Using execlp, which works the same way but with a series of strings, you would have execlp("ls","ls",NULL);

    2nd) In either case, the last element must be a NULL pointer. With execvp, that means the final element of your list.
    Code:
    #include <stdlib.h>
    #include <string.h>
    #include <unistd.h>
    
    int main() {
    	char *args[3];
    	args[0]=malloc(3);
    	strcpy(args[0],"ls");
    	args[1]=malloc(2);
    	strcpy(args[1],"/");
    	args[2]=NULL;
    	if (execvp("ls",args)==-1) perror("execvp");
    	return 0;
    }
    Notice that "ls" again appears twice in this.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. intializing an array of strings
    By doubty in forum C Programming
    Replies: 4
    Last Post: 06-19-2009, 12:59 PM
  2. Replies: 1
    Last Post: 03-19-2009, 10:56 AM
  3. Build an array of strings dynamically
    By Nazgulled in forum C Programming
    Replies: 29
    Last Post: 04-07-2007, 09:35 PM
  4. Array of strings in C
    By szill in forum C Programming
    Replies: 10
    Last Post: 02-22-2005, 04:03 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21