scanf(), Parameter Passing, and Character Arrays

L

linguae

Hello. In my C program, I have an array of character pointers. I'm
trying to input character strings to each index of the character
pointer array using scanf(), but when I run the program, I get
segmentation faults and core dumps. The problem occurs when the
program calls scanf(). I don't know what is wrong with it.

Here is my code:

#include "stdio.h"
#define SIZE 5

void input(char *string, char *string2);

int main(void)
{
char *string[SIZE], *string2[SIZE];

int counter;

for(counter = 0; counter <= SIZE - 1; counter++)
input(string[counter], string2[counter]);

putchar('\n');
for(counter = 0; counter <= SIZE - 1; counter++)
printf("%s %s\n", string[counter], string2[counter]);

return 0;
}

void input(char *string, char *string2)
{
printf("Enter string 1: ");
scanf("%s", string);
printf("Enter string 2: ");
scanf("%s", string2);
}

Thanks in advance.
 
D

Doraj

scanf("%s", string)
'string' has to be allocated (malloc, ...) before calling scanf which
is not the case in your program
Beware or the max length of the string : if someone puts more
characters than you allocated, you may get seg faults again
Hope this will help
 
F

Flash Gordon

linguae said:
Hello. In my C program, I have an array of character pointers. I'm
trying to input character strings to each index of the character
pointer array using scanf(), but when I run the program, I get
segmentation faults and core dumps. The problem occurs when the
program calls scanf(). I don't know what is wrong with it.

Here is my code:

#include "stdio.h"
#define SIZE 5

void input(char *string, char *string2);

int main(void)
{
char *string[SIZE], *string2[SIZE];

These arrays are uninitialised, so the pointers don't point anywhere in
particular.
int counter;

for(counter = 0; counter <= SIZE - 1; counter++)
input(string[counter], string2[counter]);

Passing garbage pointers to your input routine.
putchar('\n');
for(counter = 0; counter <= SIZE - 1; counter++)
printf("%s %s\n", string[counter], string2[counter]);

return 0;
}

void input(char *string, char *string2)
{
printf("Enter string 1: ");
scanf("%s", string);

Even if the pointer pointed somewhere, this is just as bad as using gets
since you are not specifying the buffer size and therefore more data
could be entered than fits giving you a buffer overflow.
printf("Enter string 2: ");
scanf("%s", string2);
}

Thanks in advance.

This is all basic stuff. You need to read up on pointers.
 
C

CBFalconer

linguae said:
Hello. In my C program, I have an array of character pointers. I'm
trying to input character strings to each index of the character
pointer array using scanf(), but when I run the program, I get
segmentation faults and core dumps. The problem occurs when the
program calls scanf(). I don't know what is wrong with it.

Here is my code:

#include "stdio.h"
#define SIZE 5

void input(char *string, char *string2);

int main(void)
{
char *string[SIZE], *string2[SIZE];

int counter;

for(counter = 0; counter <= SIZE - 1; counter++)
input(string[counter], string2[counter]);

putchar('\n');
for(counter = 0; counter <= SIZE - 1; counter++)
printf("%s %s\n", string[counter], string2[counter]);

return 0;
}

void input(char *string, char *string2)
{
printf("Enter string 1: ");
scanf("%s", string);
printf("Enter string 2: ");
scanf("%s", string2);
}

This is just the sort of thing where my ggets routine will be
handy. See:

<http://cbfalconer.home.att.net/download/ggets.zip>

and rewrite your program more or less as follows (untested)

#include <stdio.h>
#include "ggets.h"
#define SIZE 5

void input(char **str_1, char **str_2)
{
printf("Enter string 1: "); fflush(stdout);
if (0 != ggets(str_1)) puts("error");
printf("Enter string 2: "); fflush(stdout);
if (0 != ggets(str_2)) puts("error");
}

int main(void)
{
char *string[SIZE], *string2[SIZE];
int counter;

for (counter = 0; counter <= SIZE - 1; counter++)
input(&string[counter], &string2[counter]);

putchar('\n');
for (counter = 0; counter <= SIZE - 1; counter++)
printf("%s %s\n", string[counter], string2[counter]);

return 0;
}

Note the ampersands in the calls to input. BTW the exit conditions
in the for loop are more conventionally written as "counter <
SIZE".

After all that ggets will handle securing memory for your strings,
lack of which is the fundamental problem in your code.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,152
Latest member
LorettaGur
Top