0

I want to make codes which are scanning the words and print them by using a pointer array so I made this code. But when I implement it makes core dumped. Why this code make core dumped and how to fix it? Am I wrong to make code?

Then please explain to me thanks for reading my question.

#include <stdio.h>
#include <string.h>

int main(void)
{
    char*pch[5];
    int i;

    for(i=0; i<5; i++)
    {
        printf("enter word: ");
        scanf("%s", pch[i]);
    }

    for(i=0; i<5; i++)
    {
        printf("%s\n", pch[i]);
    }

    return 0;
}
Alex44
  • 3,597
  • 7
  • 39
  • 56
kangweon
  • 11
  • 1

4 Answers4

1

You cannot just allocate the pointers:

char*pch[5];

You need to allocate the memory for your strings:

char pch[5][100];
lenik
  • 23,228
  • 4
  • 34
  • 43
  • "You cannot just allocate the pointers" : is wrongly said, the program does not allocate the pointers it just define an array for them. What about to also propose `scanf("%99s", pch[i]);` to avoid UB if read word too long ? what about also to check `scanf` returns 1 to manage premature EOF ? – bruno Jun 21 '20 at 13:47
  • @bruno: Actually, that part is correctly said. In `char *pch[5]`, there are five pointers, and this declaration is a definition that allocates space for them. It does not allocate space for the pointers to point to, but it does allocate space for the pointers themselves. – Eric Postpischil Jun 21 '20 at 13:53
  • @EricPostpischil for me the sentence is very confusing, need to be clear and without ambiguity, is just wording. Note also the word 'allocate' disturb a lot of people when it is a local var (rather than a dynamic allocation in the heap). just my 2 cents – bruno Jun 21 '20 at 13:58
1

char*pch[5]; defines an array of five pointers to char. It does not assign any values to those pointers, and it does not allocate any space for them to point to.

In scanf("%s", pch[i]);, scanf must be passed a pointer to space that has already been allocated. It will not allocate space and will not change pch[i]. Because no value has been assigned to pch[i], the behavior of this statement is not defined. Commonly, scanf receives a bad address, and this causes a memory access fault.

To fix this, you can either define an array of arrays of char:

char pch[5][256];

or you can allocate space to be used with each pointer:

char *pch[5];
for (int i = 0; i < 5; ++i)
    pch[i] = malloc(256);

Either of these will let you proceed with your program, but they are suitable only for early learning purposes. As you proceed with programming, you will learn how to limit what scanf reads so that it stays within the space provided for it, how to test the return value of malloc for problems, and how to process input when it may overflow a fixed amount of space.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
0

Because you do not allocate space for the pch pointer array you defined above. About Strdup

bekici
  • 68
  • 10
0

See my comments after each section in your code.

char*pch[5];

pch is an array of 5 char pointers. each pointer in the pch points to some garbage value.

for(i=0; i<5; i++)
{
printf("enter word: ");
scanf("%s", pch[i]);
}

here you are trying to save the input string to invalid address (the garbage address). this address is not yours. if you want to save the input string make room for it. how? with malloc on the heap or on the stack. how do we allocate on the stack? see below:

Assuming you strings are at maximum 9 chars and one for the '\0', then:

char*pch[5];
char str_0[10];
char str_1[10];
char str_2[10];
char str_3[10];
char str_4[10];

pch[0]=str_0; 
pch[1]=str_1; 
...... and so on ...

you can allocate the whole strings array on the heap:

pch[0] = (char*)malloc(sizeof(char) * 10);
... and so on...

In malloc case you need to free all buffers (by calling free() at the end)

Adam
  • 2,820
  • 1
  • 13
  • 33