0

I have a program which takes in the user input for classroom and time. The classroom input is store inside a char* classroom, and the time is store inside a int time. However, when I run the program, it stops at when I press enter after I have type a classroom. The printf for "Please enter a time: ", did not come out. Why is this so??

void option1()
{
  char* classroom; 
  int time;        

  printf("Please enter the classroom: ");
  scanf_s("%s", &classroom); 

  printf("Please enter the time: ");
  scanf_s("%d", &time);
}

Thanks for help :)

Daaenerys
  • 11
  • 1
  • 5
  • `scanf_s` works differently, you're looking for `scanf`. –  Jul 27 '17 at 11:04
  • `classroom` is already a pointer, don't use the `&` operator here. –  Jul 27 '17 at 11:05
  • 1
    `"%s"` is a guaranteed buffer overflow, always use a field-width. –  Jul 27 '17 at 11:05
  • 1
    `classroom` doesn't point anywhere, you didn't allocate memory for it -> *undefined behavior*. –  Jul 27 '17 at 11:06
  • @FelixPalmen and classroom points where = undefined behavior? Ah, just faster :) – unalignedmemoryaccess Jul 27 '17 at 11:06
  • @tilz0R I wasn't done yet. One comment per defect ;) –  Jul 27 '17 at 11:06
  • then is there other way to store the user input for class room? – Daaenerys Jul 27 '17 at 11:07
  • Most of the time, `scanf()` isn't a good idea anyways. You might be interested in my [beginners' guide away from `scanf()`](http://sekrit.de/webdocs/c/beginners-guide-away-from-scanf.html) –  Jul 27 '17 at 11:14

4 Answers4

2

As I already commented, there are a lot of errors in this snippet:

  • scanf_s is by no means the same as scanf, it needs additional size parameters. I'd suggest to not use it.
  • Never use just "%s" with scanf(), you need to specify a field width to prevent a buffer overflow. (This is different for scanf_s() because the buffer size is an additional parameter there.)
  • You try to write data through a pointer (classroom) that points nowhere, you need to allocate memory! (either by making this an array or by calling malloc()).

Your snippet with these errors corrected might look like:

void option1()
{
  char classroom[128];
  int time;        

  printf("Please enter the classroom: ");
  scanf("%127s", classroom);
  // field width is one less than your buffer size,
  // because there will be a 0 byte appended that terminates the string!

  printf("Please enter the time: ");
  scanf("%d", &time);
}
0

Either create variable as pointer and allocate memory for it or allocate char array and pass it to scanf.

//Use of array
char classroom[10];
scanf("%9s", classroom); 

//Use of heap
char* classroom = malloc(10 * sizeof(*classroom));
scanf("%9s", classroom); 
//Use your variable classroom here, when done, call free to release memory.
free(classroom);

9 is here as string length to prevent buffer overflow and out of bounds writing. Array has size of 10 elements.

unalignedmemoryaccess
  • 7,246
  • 2
  • 25
  • 40
0

Use scanf instead of scanf_s, allocate memory for the char pointer and don't use the &, it is already a pointer. See example below:

#include <stdio.h>
#include <stdlib.h>

int main()
{
  int time;        
  char* classroom; 
  classroom = malloc(sizeof(char) * 1024);

  if(classroom == NULL)
  {
    fprintf(stderr, "Failed to allocate memory!\n");
    exit(1);
  }

  printf("Please enter the classroom: ");
  scanf("%s", classroom); 

  printf("Please enter the time: ");
  scanf("%d", &time);


  printf("\nClassroom: %s\n", classroom);
  printf("Time: %d\n", time);

  free(classroom); //Free the memory

  return 0;
}
John
  • 652
  • 7
  • 22
0

The scanf family of functions read the data specified by the format string into the variable(s) provided as a parameter(s).

These functions do not allocate storage for a string variable! Providing a variable classroom to scanf without having alocated memory for it, will make scanf attempt to place the data at an undefined place in memory. This can result in any type of behavior, generally called undefined behavior.

So you must first allocate storage for your string variable, for example:

char*classroom= malloc(1024);

an now you can call scanf with it:

scanf_s("%s", classroom);

Note that, because a char pointer behaves as an array (i.e. it points to an array of char), you don't use the address of operator, &.

Paul Ogilvie
  • 25,048
  • 4
  • 23
  • 41
  • hmm, there is a problem with the malloc part: Severity Code Description Project File Line Suppression State Error (active) a value of type "void *" cannot be used to initialize an entity of type "char *" – Daaenerys Jul 27 '17 at 11:27
  • 1
    @Daaenerys, you probably compile as c++. In c this is fine. – Paul Ogilvie Jul 27 '17 at 11:31