2
votes

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.

3
I'm guessing you should add the "homework" tag.Hot Licks
"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

3 Answers

3
votes

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

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

2
votes

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;
}
1
votes

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