By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,885 Members | 1,469 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,885 IT Pros & Developers. It's quick & easy.

scanf(), Parameter Passing, and Character Arrays

P: n/a
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.

Nov 14 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
> 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

Nov 14 '05 #2

P: n/a
linguae wrote:

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.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
Nov 14 '05 #3

P: n/a
linguae wrote:
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.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Nov 14 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.