Difference between scanf for char * and char [ ]?

wn5865

I'm a newbie to c programming. I'm trying to input two strings using scanf. My first try was as below

#include <stdio.h>
int main(void)
{
    char *word1;
    char *word2;
    scanf("%s", word1);
    scanf("%s", word2);
    printf("%s\n", word1);
    printf("%s\n", word2);
}

If I run this code, only the first input is correctly stored (word2 is null). But, if I run the code below, both inputs are correctly stored in word1 and word2.

#include <stdio.h>
int main(void)
{
    char word1[10];
    char word2[10];
    scanf("%9s", word1);
    scanf("%9s", word2);
    printf("%s\n", word1);
    printf("%s\n", word2);
}

What is the problem with using pointers with scanf?

H.S.

What is the problem with using pointers with scanf?

The first one is undefined behaviour. Look at this part of code:

char *word1;
char *word2;
scanf("%s", word1);
scanf("%s", word2);

No memory allocated to word1 and word2 pointers. When you give input, scanf() end up accessing unallocated pointers and dereferencing an unallocated/invalid pointer is undefined behaviour.

You should make sure that before using/accessing a pointer, it should be pointing to a valid memory location. You can either make it point to an exiting valid memory or allocate memory dynamically to it, like this:

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

int main(void)
{
    char *word1;
    char *word2;

    word1 = malloc (10);
    if (word1 == NULL) {
        exit (EXIT_FAILURE); // or whatever you want to do to handle memory allocation failure
    }
    word2 = malloc (10);
    if (word2 == NULL) {
        exit (EXIT_FAILURE); // or whatever you want to do to handle memory allocation failure
    }

    scanf("%9s", word1);
    scanf("%9s", word2);
    printf("%s\n", word1);
    printf("%s\n", word2);

    // once done with allocated memory, free it
    free (word1); 
    free (word2);

    return 0;
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

The difference between char * and char[]

From Dev

Difference between char** and char[][]

From Dev

Difference between char[] and char * in C

From Dev

Difference between char* and char[] (2)

From Dev

Difference between int*a and char *a?

From Dev

difference between char [] and char* when doing strcpy

From Java

What is the difference between char s[] and char *s?

From Dev

What is the difference between char array[] and char *array?

From Dev

What is the difference between char and unsigned char?

From Dev

Difference between new String(char[]) and char[].toString

From Dev

Difference between string literal and const char[], or char[]

From Dev

What is the difference between char array[] and char *array?

From Dev

Difference between new String(char[]) and char[].toString

From Dev

Difference between char, char* inside Linked Lists

From Dev

Difference between char **p,char *p[],char p[][]

From Java

Difference between char and character objects

From Dev

Difference between 2D char Array and char** (OR, 3D char Array and char*** etc)

From Dev

Difference between 2D char Array and char** (OR, 3D char Array and char*** etc)

From Java

What is the difference between returning a char* and a char[] from a function?

From Dev

How to tell difference between dynamically allocated char* and static char*

From Dev

What is the difference between char **ptr and char *ptr[] in C?

From Java

Why is there now a difference between "{static const char a[]={...}" and "{const char a[]={...}"?

From Dev

What's the difference between char[] & char* when declare inside struct?

From Dev

Difference between istream::get(char&) and operator>> (char&)

From Dev

What's the difference between char[] & char* when declare inside struct?

From Dev

Difference between "char[] name" and "char name[]" as a function's argument in c

From Dev

What is difference between char name[11]; and char *name;

From Dev

What is the difference between char stringA[LEN] and char* stringB[LEN] in C

From Dev

C scanf char array

Related Related

  1. 1

    The difference between char * and char[]

  2. 2

    Difference between char** and char[][]

  3. 3

    Difference between char[] and char * in C

  4. 4

    Difference between char* and char[] (2)

  5. 5

    Difference between int*a and char *a?

  6. 6

    difference between char [] and char* when doing strcpy

  7. 7

    What is the difference between char s[] and char *s?

  8. 8

    What is the difference between char array[] and char *array?

  9. 9

    What is the difference between char and unsigned char?

  10. 10

    Difference between new String(char[]) and char[].toString

  11. 11

    Difference between string literal and const char[], or char[]

  12. 12

    What is the difference between char array[] and char *array?

  13. 13

    Difference between new String(char[]) and char[].toString

  14. 14

    Difference between char, char* inside Linked Lists

  15. 15

    Difference between char **p,char *p[],char p[][]

  16. 16

    Difference between char and character objects

  17. 17

    Difference between 2D char Array and char** (OR, 3D char Array and char*** etc)

  18. 18

    Difference between 2D char Array and char** (OR, 3D char Array and char*** etc)

  19. 19

    What is the difference between returning a char* and a char[] from a function?

  20. 20

    How to tell difference between dynamically allocated char* and static char*

  21. 21

    What is the difference between char **ptr and char *ptr[] in C?

  22. 22

    Why is there now a difference between "{static const char a[]={...}" and "{const char a[]={...}"?

  23. 23

    What's the difference between char[] & char* when declare inside struct?

  24. 24

    Difference between istream::get(char&) and operator>> (char&)

  25. 25

    What's the difference between char[] & char* when declare inside struct?

  26. 26

    Difference between "char[] name" and "char name[]" as a function's argument in c

  27. 27

    What is difference between char name[11]; and char *name;

  28. 28

    What is the difference between char stringA[LEN] and char* stringB[LEN] in C

  29. 29

    C scanf char array

HotTag

Archive