473,563 Members | 2,662 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 14128
> 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.c om, 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
10077
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 code... TCHAR myArray; DoStuff(myArray);
39
100755
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 anything even a hint would be really helpful, I cant for the life of me see why the 2nd prog wont work... gets before scanf ...
6
599
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 just a representation, when I print each character in the two char arrays, Key1 displays ok but Ptext is missing the first element. I'm stumped can...
11
2897
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
3089
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 help me in finding why such thing is happening and what the remedy to it is. Kindly bear with my English. int main ()
30
3163
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 of the C FAQ says that it's wrong for that very reason. What I don't know is where the standard tells me conclusively that it's wrong. What I also...
62
4981
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 don't understand how could scanf() affect the content of i and i. Can someone tell me why? #include <stdio.h> #include <ctype.h> void main()...
26
4500
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
6539
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
7659
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7882
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8103
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7634
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7945
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
3634
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3618
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2079
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1194
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.