Thread: array of pointer to string

  1. #1
    Registered User
    Join Date
    Aug 2012
    Posts
    23

    array of pointer to string

    how to take user input stringand store it in array

  2. #2
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Code:
    char storage[100];
    ...
    strcpy(storage, userinput);
    You did say 'string' which means it's nul terminated. You must ensure that the destination is large enough.

  3. #3
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by lovelycse View Post
    how to take user input stringand store it in array
    I'll give an alternative answer to the above vague question


    Code:
    char * storage[100];
    ...
    storage[i] = strdup( userinput);
    Kurt

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by lovelycse View Post
    how to take user input stringand store it in array
    For user's input, (where errors are likely):

    char inputstr[80];
    fgets(inputstr, sizeof(inputstr), stdin);

    The newline: '\n', and end of string char: '\0', will automatically be added to the end of the string array, by fgets(). (space permitting).

    Early chapters of C books for beginners, will usually use
    scanf("%s", inputstr);

    scanf() will append the end of string char, and will NOT add the newline char to the string.

    The big advantage of fgets() is that the user can't add too many char's, into the receiving char array.

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    You can also use the width parameter of scanf() to enforce the size limitation.
    Code:
    char s[100]
    scanf ("%100s",str);
    But one of the problems with scanf(), compared to fgets() is that scanf() stops processing when it encounters a space, fgets() doesn't stop until it encounters the end of line character.

    Jim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. dynamic string pointer array
    By klmdb in forum C Programming
    Replies: 7
    Last Post: 07-26-2010, 04:01 AM
  2. is a string in C = pointer = array?
    By winggx in forum C Programming
    Replies: 1
    Last Post: 03-21-2010, 11:54 PM
  3. qsort() in Array of Pointer to String
    By vb.bajpai in forum C Programming
    Replies: 8
    Last Post: 06-16-2007, 04:18 PM
  4. Pointer to array of string and Array of Pointer to String
    By vb.bajpai in forum C Programming
    Replies: 2
    Last Post: 06-15-2007, 06:04 AM
  5. string to pointer array
    By pankleks in forum C Programming
    Replies: 7
    Last Post: 02-09-2005, 06:45 AM