how to assign a string into a cell of an array

This is a discussion on how to assign a string into a cell of an array within the C Programming forums, part of the General Programming Boards category; hi I have a problem, using an aray. I'm trying to asign for example a string "susana" to the fist ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    1

    Question how to assign a string into a cell of an array

    hi
    I have a problem, using an aray.
    I'm trying to asign for example a string "susana" to the fist cell
    into an array(for example arrayName[20])

    char arrayName[20];
    printf("enter name:\n");
    scanf("%s",arrayName[0]);
    //after that I'm trying
    printf("%s",arrayname[0]);
    and the program crashed.
    my question is, can I assign a string into a cell of array and after that to display the cell with the string inside?(for example it is posible in VB)
    thanks

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    What do you mean "Into the cell of an array"?

    Do you want an array of strings? Otherwise, a "cell" of your array only contains a single character. If you want the "cell" to contain a whole string, you need an array of character pointers. Then you need to actually get into dynamicly allocating space and what not. Not that difficult, but it can be tricky.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    51
    I don't know about VB, but I think your problem is you are using a character when you need a string and vice-versa.

    arrayName[0] = a single character(the first letter)

    arrayName = string(actually a pointer to the array of chars that make up the string.)

    Try this:

    Code:
    char arrayName[20]; 
    printf("enter name:   "); 
    gets(arrayName); 
    printf("\n%s", arrayname);
    I didn't have a compiler to check this, but I think it is right.

    If you really want to use scanf() instead of gets(), try:

    Code:
    scanf("%s", arrayName);
    Last edited by drharv; 04-23-2002 at 09:48 PM.

  4. #4
    Unregistered
    Guest
    You can also use fgets so you do not overrun the size of the array


    char arrayName[20];
    printf( "Enter Name:" );
    fgets( name, sizeof( name ), stdin );

    Now the user can't enter any more characters than you have provided storage for. This is one alternative to scanf.


    If you want to use scanf & gets you might want to throw this in there between scanf & gets

    while( getchar( ) != '\n' );

    I hope this helps you.
    kris

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    It looks like you want a two dimensional array or an array of pointers:
    Code:
    char a[2][20];
    fgets ( a[0], sizeof a[0], stdin );
    printf ( "%s", a[0] );
    Code:
    char *a[2];
    if ( ( a[0] = malloc ( 20 * sizeof *a ) ) != NULL ) {
      fgets ( a[0], 20, stdin );
      printf ( "%s", a[0] );
    }
    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  2. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  3. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  4. 3-d array assign string values
    By WaterNut in forum C++ Programming
    Replies: 8
    Last Post: 07-01-2004, 12:02 AM
  5. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-26-2003, 11:03 PM

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