17

I have written the following piece of code:

int main() {
  char arrays[12];
  char *pointers;
  scanf("%s", arrays);
  scanf("%s", pointers);
  printf("%s", arrays);
  printf("%s", pointers);
  return 0;
}

Why does it give an error when I write scanf("%s", pointers)?

Hamid Rouhani
  • 2,309
  • 2
  • 31
  • 45
Saurabh.V
  • 320
  • 1
  • 2
  • 9

6 Answers6

16
char *pointers;

must be initialized. You can not scan string into pointers until you point it to some address. The computer needs to know where to store the value it reads from the keyboard.

int main() {
  char arrays[12];
  char *pointers = arrays;
  scanf("%s", pointers);
  printf("%s", pointers);
  return 0;
}
Hamid Rouhani
  • 2,309
  • 2
  • 31
  • 45
Junior Fasco
  • 227
  • 2
  • 11
10

Because you're writing to an address in memory that has not been initialized. Writing to memory pointer by an uninitialized pointer invokes undefined behaviour. Either allocate enough memory:

pointers = malloc(256);
if(!pointers)
  perror("malloc");
else
  scanf("%255s", pointers);

Or declare it as a static array:

char pointers[256];

You should also consider using fgets() instead of scanf().

You may want to read i you are interested in fgets():

Difference between scanf() and fgets()

Community
  • 1
  • 1
Gigi
  • 28,163
  • 29
  • 106
  • 188
  • "*pointers by default points to NULL" This is such a fallacy. – autistic Jan 27 '13 at 09:23
  • sure? pointer is not guaranted to be NULL, it is not a static variable – Emanuele Paolini Jan 27 '13 at 09:27
  • @KingsIndian, the code in that edit is severely broken. In addition, scanf will behave differently to fgets when you give it the %s format specifier. – autistic Jan 27 '13 at 09:44
  • 2
    @modifiablelvalue Fixed the typos. I only suggested fgets(). I didn't say they are equivalent and alternative for each other. I expect anyone to read manual before using if the person is unaware of how a standard function works. – P.P Jan 27 '13 at 09:59
  • @modifiablelvalue hehe, anyway I added link to manual and another question describing differences :) – P.P Jan 27 '13 at 10:11
5
  • char *pointers; creates a pointer variable.
  • pointers is the address pointed to by pointers, which is indeterminate by default.
  • *pointers is the data in the address pointed to by pointers, which you cannot do until address is assigned.

Just do this.

char arrays[12];
char *pointers;
pointers = arrays;
scanf("%s",pointers);
ATOzTOA
  • 34,814
  • 22
  • 96
  • 117
3

pointers is being used without initialisation, like int x; printf("%d\n", x);. You need to make your pointer point to something before using it. Which book are you reading?

autistic
  • 1
  • 3
  • 35
  • 80
1

pointers is an unitialized pointer. You are not able to write into it. You shall allocate enough memory to store a string, as you did with arrays. With a pointer, it is possible to use dynamic allocation (cf. malloc).

md5
  • 23,373
  • 3
  • 44
  • 93
0

Could you elaborate on the error, i'm not around a compiler right now.

But for scanf and printf to work you must have this at the top of your program:

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

Both are standard libraries for C. IO contains scanf, I'm fairly sure printf is in the same. But until you know which libraries you need for which functions it doesn't hurt to include both standard libraries for every program. Try to use custom header files as well so you don't need mass #includes for every file.

Don't forget malloc statements for memory allocation.

But I'm unsure what you're attempting to do with your code, please elaborate?

thepratt
  • 323
  • 1
  • 13