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.
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
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: - char e[3];
-
...
-
scanf("%2s",e);
Where the field width is one less than the size of the buffer (to allow for the terminating null character).
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?
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.
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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....
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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*/...
|
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...
|
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...
|
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...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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...
|
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...
|
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)...
|
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....
|
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
|
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...
| |