Thread: Two Dimensional Array Of Characters

  1. #16
    Registered User
    Join Date
    May 2006
    Posts
    17
    you need to do

    Code:
    if(fgets(arrayA[0], sizeof(arrayA[0]), stdin) != NULL)
    because arrayA is type char**, fgets is expecting type char*.
    sizeof(arrayA) will give you the whole 2d array, you only want the size of one dimension.

  2. #17
    Registered User
    Join Date
    May 2006
    Posts
    17
    Code:
    int Info(char arrayA [][9], int arrayB[], int count)
    {
    
    	printf("Enter the code for %d:  ", count);
    	fgets(arrayA[0], sizeof(arrayA[0]), stdin);
    	printf("Enter the time %d:  ", count);
    	scanf("%d", arrayB);
    }
    count is 1 btw...

    output:

    Enter the code for 1: Enter the time for 1: 2 (user input)
    Enter the code for 2: Enter the time for 2: 2 (user input)
    1 1 ☻
    2 2 ☻
    Press any key to continue...

    It doesnt let me enter the code, it just skipps to the next part where i enter the time.
    any ideas?

  3. #18
    Registered User
    Join Date
    May 2006
    Posts
    17
    that's strange, I can't see why it wouldn't work.

    Of course if you are reading multiple things in you will need to keep track of which element in the array you are up to, so you will need to do

    fgets(arrayA[i], sizeof(arrayA[i]), stdin)

    and increment i accordingly.

    I compiled it and ran it and it worked fine for me.
    Last edited by jarro_2783; 05-24-2006 at 02:55 AM.

  4. #19
    Awesomefaceradcore bivhitscar's Avatar
    Join Date
    Apr 2006
    Location
    Melbourne, Australia
    Posts
    210
    > It doesnt let me enter the code, it just skipps to the next part where i enter the time.

    This is where I used to suggest fflush, but that is bad apparantly. Use fgets and sscanf to get the time

    Code:
    int Info(char arrayA [][9], int arrayB[], int count)
    {
    	char buf[BUFSIZ];
    	
    	printf("Enter the code for %d:  ", count);
    	fgets(arrayA[0], sizeof(arrayA[0]), stdin);
    	printf("Enter the time %d:  ", count);
    	fgets( buf, BUFSIZ, stdin);
    	sscanf( buf, "%d", arrayB);
    }
    Though, you're always writing into the first element of each array, which I imagine is not what you want to do...
    Last edited by bivhitscar; 05-24-2006 at 03:10 AM.
    it's ironic considerate rarity patron of love higher knowledge engulfs me...

  5. #20
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    There seems to be a lot of confusion over what pointer to arrays are, especially the difference between a ** and [][N] parameter.

    Code:
    #include<stdio.h>
    
    void info(char arrayA[][9], int arrayB[], int count ) {
      char buff[BUFSIZ];
    
      printf( "pointer=%u\n", sizeof(arrayA) );
      printf( "array row (char[9])=%u\n", sizeof(arrayA[0]) );
      printf( "single element (char)=%u\n", sizeof(arrayA[0][0]) );
      printf( "pointer=%u\n", sizeof(arrayB) );
      printf( "single element (int)=%u\n", sizeof(arrayB[0]) );
    
      printf("Enter the code for %d:  ", count);
      fgets( buff, BUFSIZ, stdin );
      sscanf( buff, "%8s", arrayA[count] );
      printf("Enter the time %d:  ", count);
      fgets( buff, BUFSIZ, stdin );
      sscanf( buff, "%d", &arrayB[count] );
    }
    
    int main ( void ) {
      char arrayA[5][9];
      int arrayB[5];
      info( arrayA, arrayB, 1 );
      printf( "%s %d\n", arrayA[1], arrayB[1] );
      return 0;
    }
    
    
    $ ./a.exe
    pointer=4
    array row (char[9])=9
    single element (char)=1
    pointer=4
    single element (int)=4
    Enter the code for 1:  hello
    Enter the time 1:  4
    hello 4
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Zero out two dimensional array
    By davo666 in forum C Programming
    Replies: 16
    Last Post: 01-08-2009, 05:28 AM
  2. Multi dimensional array
    By $l4xklynx in forum C Programming
    Replies: 7
    Last Post: 01-03-2009, 03:56 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. 2 dimensional char array question
    By knutso in forum C++ Programming
    Replies: 4
    Last Post: 12-05-2002, 06:28 AM