2

I have already looked at other related questions, and none of them helped this case.
I am getting the warning listed in the title of my question, and my code for main is as follows:

int main( int argc, char *argv[] ) {

  char *rows;  
  int i, n;  

  printf("\nEnter the amount of rows in the telephone pad: ");  
  scanf("%d", &n);  

  rows = (char *) malloc( sizeof( char ) * n );  

  printf("\nNow enter the configuration for the pad:\n");  
  for( i = 0; i < n; i++ ) {  
    scanf("%s", &rows[i]);  
    printf("\n\t%s\n", rows[i]);  
  }  

  return 0;    
}

The user is to enter a number (say, 4), which will be scanned into n. The space is malloc'ed for the rows of the telephone pad. The user then will enter the n amount of rows for the configuration of the telephone pad. An example would be:

123
456
789
.0.

So I am confused as to why my last printf statement is getting this error.

Note: I also tried scanf("%s", rows[i]);: still got the error.
Note 2: I tried running the program anyways. Got a segmentation fault.
Note 3: I have #include <stdio.h> and #include <stdlib.h> at the top of my .c program.
Note 4: I have gcc'ed the program as such: gcc -ansi -pedantic -Wall tele.c.

Thank you for the help.

Mxyk
  • 10,678
  • 16
  • 57
  • 76
  • I'm guessing you should add the "homework" tag. – Hot Licks Dec 06 '11 at 02:14
  • "So I am confused " -- I'm baffled by that use of the word "so", as nothing you wrote before it has any bearing on the error message you got. The way to go about these things is to drop all your preconceptions and take error messages and what they are telling you seriously. Here, it tells you that `%s` expects an argument of type `char*`, which is of course true, and it also tells you that the argument you provided is instead of type `int` ... which is also certainly true, because `rows[i]` is a `char`, which gets promoted to `int` when passed to a varargs function. – Jim Balter Dec 06 '11 at 04:20

3 Answers3

3

rows[i] isn't a char* -- it's not a "string".

(And you can't fit 3 characters (plus null terminator) in one character.)

Hot Licks
  • 47,103
  • 17
  • 93
  • 151
  • So I should change it to `char **rows`, correct? What should do about the `malloc` statement and `scanf` statement? – Mxyk Dec 06 '11 at 02:07
  • if you want that, you want char** and malloc it to be n * sizeof(char) * the_length_of_string_you_expect_the_user_to_enter – Keith Nicholas Dec 06 '11 at 02:12
  • That didn't work out for me. When I ran the program, I entered 5 for n, and as soon as I entered '123', I got (null) printed 5 times, then the program ended. I changed rows to be a char** and malloc to be `rows = (char **) malloc( sizeoof( char * ) * n * 11 );`, where 10 is the largest length of the string the user should be inputting, plus the null byte. – Mxyk Dec 06 '11 at 02:18
  • I did do that, haha. I still don't get why it'd print null 5 times then crash. – Mxyk Dec 06 '11 at 02:26
  • Well nevermind, I somehow got it to work. Made a silly mistake somewhere :). Thank you for the help! – Mxyk Dec 06 '11 at 02:38
  • If you don't have an array of strings you're gonna be busted at some point. You need to do N+1 mallocs. – Hot Licks Dec 06 '11 at 02:43
2

As others have pointed out, you are allocating space for n chars, but you really want space for n rows with 4 chars each (3 characters entered by the user, and a null terminator).

There's a couple of ways to do this. You can first allocate n char * variables to point to the rows, then allocate 4 bytes for each row:

int main( int argc, char *argv[] )
{
  char **rows;
  int i, n;

  printf("\nEnter the amount of rows in the telephone pad: ");
  scanf("%d", &n);

  rows = malloc( n * sizeof rows[0] );

  printf("\nNow enter the configuration for the pad:\n");
  for( i = 0; i < n; i++ ) {
    rows[i] = malloc(4);
    scanf("%3s", rows[i]);
    printf("\n\t%s\n", rows[i]);
  }

  return 0;
}

Or, you can allocate n 4-character arrays up front:

int main( int argc, char *argv[] )
{
  char (*rows)[4];
  int i, n;

  printf("\nEnter the amount of rows in the telephone pad: ");
  scanf("%d", &n);

  rows = malloc( n * sizeof rows[0] );

  printf("\nNow enter the configuration for the pad:\n");
  for( i = 0; i < n; i++ ) {
    scanf("%3s", rows[i]);
    printf("\n\t%s\n", rows[i]);
  }

  return 0;
}
caf
  • 233,326
  • 40
  • 323
  • 462
1

your printf is passing in a char, not a %s

you are saying get the i'th char in the string 'rows'.

More importantly, your whole technique is going to fail badly....

I think you want an array of strings.... or you want to change your scanf to a %c

Keith Nicholas
  • 43,549
  • 15
  • 93
  • 156
  • I want an array of strings. Could you answer the comment I put on Hot Licks answer? Thank you. – Mxyk Dec 06 '11 at 02:10