473,320 Members | 1,814 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,320 software developers and data experts.

Null issues raised by scanf

Hello everyone,

I am a beginer of C programming and have a short question here.
I declared my variables as bellow in the main method.

char a[10];
char b[25];
char c[25];
char d[5];
char e[3];

scanf("%s%s%s%s%s",a,b,c,d,e);

printf("%s\t%s\t%s\t%s\t%s\t",a,b,c,d,e);


when I do the scanf, my inputs are
a
b
c
A80
P01

(I have them in the same line when I input the values, here just for make them more clearly to read.)


My problem is that after the printf() executed, the outputs are
a
b

A80
P01.

The variable c is supposed not to be null. It should have its own value, which is 'c' in this case. I am wondering what causes the null pointer and what is the solution?

Your help will be mostly gratitude. Thanks all in advance.
Apr 5 '10 #1

✓ answered by donbock

Suppose the compiler organizes memory such that the c array immediately follows the e array. Four characters ('P', '0', '1', and '\0') are written into the e array, but since it is only large enough to hold three characters, the last one overflows into the next variable. Thus, a null character is written into the first slot of the c array.

I don't know if that is what's happening, but it is plausible and it would explain your symptoms. Fix the overflows. If you still have problems then we'll keep investigating.

5 2460
donbock
2,426 Expert 2GB
The e array is not large enough to hold the entered string. The resulting overflow leads to undefined behavior.

Follow this convention to prevent buffer overflow:
Expand|Select|Wrap|Line Numbers
  1. char e[3];
  2. ...
  3. scanf("%2s",e);
Where the field width is one less than the size of the buffer (to allow for the terminating null character).
Apr 5 '10 #2
Thanks for your answer.
However, it doest not solve my problem. Actually, my array e works properly, i think, as it contains the string which i input. (in this case, it's P01) The issue is in array C, for nothing is in it. Any new ideas?
Apr 6 '10 #3
donbock
2,426 Expert 2GB
Suppose the compiler organizes memory such that the c array immediately follows the e array. Four characters ('P', '0', '1', and '\0') are written into the e array, but since it is only large enough to hold three characters, the last one overflows into the next variable. Thus, a null character is written into the first slot of the c array.

I don't know if that is what's happening, but it is plausible and it would explain your symptoms. Fix the overflows. If you still have problems then we'll keep investigating.
Apr 6 '10 #4
yeah.. i think u r definitely right. I fixed the problem by set all the sizes of array to 80, though i think this may be kind of a waste of memory.
By the way, array in c language, especially in char[] type is so different from other languages'. Sometimes, i am sure that there should be only 10 elements in the array but i have to initialize the size of array to be a very large number, for instance 30. Otherwise, fragmentation faults will be raised. Do u know exactly how to exactly set the array size according to a specific need rather than just giving a big number for considerations of safety.
Apr 7 '10 #5
jkmyoung
2,057 Expert 2GB
When reading in your input with scanf, set width modifiers for the maximum amount of input allowed in.

Reference: http://www.cplusplus.com/reference/c.../cstdio/scanf/
Example: http://www.tenouk.com/clabworksheet/labworksheet6.html
Garbage in, garbage out.
Apr 7 '10 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

12
by: B Thomas | last post by:
Hi, I was reading O'Reilly's "Practical C programming" book and it warns against the use of scanf, suggesting to avoid using it completely . Instead it recomends to use using fgets and sscanf....
7
by: hugo27 | last post by:
obrhy8 June 18, 2004 Most compilers define EOF as -1. I'm just putting my toes in the water with a student's model named Miracle C. The ..h documentation of this compiler does state that when...
29
by: Jason Curl | last post by:
I've been reading this newsgroup for some time and now I am thoroughly confused over what NULL means. I've read a NULL pointer is zero (or zero typecast as a void pointer), others say it's...
42
by: junky_fellow | last post by:
Consider an implementation that doesn't use all bits 0 to represent a NULL pointer. Let the NULL pointer is represented by 0x12345678. On such an implementation, if the value of NULL pointer is...
9
by: Thomas Mlynarczyk | last post by:
Hi, It seems to be a generally adopted convention to have a function return FALSE in case of an error. But if a function is supposed to return a boolean anyway, one cannot distinguish anymore...
10
by: Robert | last post by:
I have an app that was originally 1.1, now migrated to 2.0 and have run into some sporadic viewstate errors...usually saying the viewstate is invalid, eventvalidation failed or mac error. My web...
35
by: fcvcnet | last post by:
Hi, This is c code , but the question is same to c++ £¨new/delete£©. char* p = malloc(10); strcpy(p, "hello"); printf("%s\n", p); free(p); // free just add p to the free list of memory, and...
14
by: main() | last post by:
I know this is the problem that most newbies get into. #include<stdio.h> int main(void) { char a; scanf("%c",&a); /*1st scanf */ printf("%c\n",a); scanf("%c",&a); /*2nd scanf*/...
0
by: Aaron Morton | last post by:
I'm working on a IHttpModule that handles the PreSendRequestHeaders event from the HttpApplication, if the event is raised after EndRequest then HttpContext.Current is null. If it is raised before...
11
by: MikeT | last post by:
This may sound very elementary, but can you trap when your object is set to null within the object? I have created a class that registers an event from an object passed in the constructor. When...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.