you need to do
because arrayA is type char**, fgets is expecting type char*.Code:if(fgets(arrayA[0], sizeof(arrayA[0]), stdin) != NULL)
sizeof(arrayA) will give you the whole 2d array, you only want the size of one dimension.
you need to do
because arrayA is type char**, fgets is expecting type char*.Code:if(fgets(arrayA[0], sizeof(arrayA[0]), stdin) != NULL)
sizeof(arrayA) will give you the whole 2d array, you only want the size of one dimension.
count is 1 btw...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); }
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?
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.
> 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
Though, you're always writing into the first element of each array, which I imagine is not what you want to do...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); }
Last edited by bivhitscar; 05-24-2006 at 03:10 AM.
it's ironic considerate rarity patron of love higher knowledge engulfs me...
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.