473,395 Members | 1,869 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

scanf(), Parameter Passing, and Character Arrays

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
3 14117
> 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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
39
by: Teh Charleh | last post by:
OK I have 2 similar programmes, why does the first one work and the second does not? Basically the problem is that the program seems to ignore the gets call if it comes after a scanf call. Please...
6
by: Pete | last post by:
Following is a fragment from a program I'm writing. I am trying to enter an 8 bit binary representation from keyboard which is then scanfed into &Ptext and then again into &Key1. This is not binary...
11
by: QQ | last post by:
Hello I am running this code int main(void) { char A,B; printf("Please input A: \n"); scanf("%2s",A); printf("Please input B: \n"); scanf("%2s",B); printf("A is %s,B is %s\n",A,B); }
33
by: Lalatendu Das | last post by:
Dear friends, I am getting a problem in the code while interacting with a nested Do-while loop It is skipping a scanf () function which it should not. I have written the whole code below. Please...
30
by: James Daughtry | last post by:
char array; scanf("%19s", &array); I know this is wrong because it's a type mismatch, where scanf expects a pointer to char and gets a pointer to an array of 20 char. I know that question 6.12...
62
by: Argento | last post by:
I was curious at the start about how ungetc() returns the character to the stream, so i did the following coding. Things work as expected except if I change the scanf("%c",&j) to scanf("%d",&j). I...
26
by: tesh.uk | last post by:
Hi Gurus, I have written the following code with the help of Ivor Horton's Beginning C : // Structures, Arrays of Structures. #include "stdafx.h" #include "stdio.h" #define MY_ARRAY 15
2
by: subramanian100in | last post by:
Consider the following program named as x.c #include <stdlib.h> #include <stdio.h> int main(void) { unsigned int u; char str;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.