Question:
c programming difficulty.......?
anonymous
1970-01-01 00:00:00 UTC
c programming difficulty.......?
Eight answers:
anonymous
2011-06-20 04:47:38 UTC
You're still making stupid errors.



cName's size is five chars and you're copying far more than five to the beginning of cName. Turbo C (apparently) lays out memory like so:



cName: [ 'e', 'x', 'a', 'm', '\0' ] iNo: [ 00, 10, 00, 20, 00, 30, 00, 40 ]



and after the copy:



cName: [ 'u', 'n', 'i', 'v', 'e' ] iNo: [ 'r', 's', 'i', 't', 'y', '\0', 00, 40 ]



You're writing to the first few bits of iNo by overflowing the array cName. Re-define your variables like so:



int iNo[4] = { 10, 20, 30, 40 };

char cName[512] = "exam";

char cMidd[] = "university";



Also, re-define your main function like so:



int main()

{

...

return 0;

}



And try not to use crap like clrscr() and getch() when they're not necessary.



EDIT: You should switch to a compiler which has actually heard of C99 like gcc, tendra, clang, or even tcc. Turbo C's last release was in 1989 (nearly 22 years ago). It's outdated and encourages you to do stupid crap like void main(), getch, and clrscr.
?
2016-10-05 12:01:38 UTC
Yeah you may the two upload the ".h" extension like all human beings pronounced, or you may pass away that off and stick (on the subsequent line) "utilising namespace std;" (without the charges). additionally you do not choose the "std::". purely say "cout << "help;"" by utilising the way, i like to adhere a "<< endl;" on the top of all my couts. It makes your software prettier (like hitting enter). So right this is what it would look like: the two, #comprise int considerable() { cout << "help" << endl; return 0; } OR #comprise iostream utilising namespace std; int considerable() { cout << "help" << endl; return 0; } It truly relies upon on the compiler. it could take the two, or it could basically take one in each and every of them.
cosimo
2011-06-20 03:56:55 UTC
The problem is with the strcpy() call.



In my compiler (MinGW) that variables iNo, cName and cMidd are placed at the following locations in memory.



iNo 0x28FF20

cName 0x28FF1B

cMidd 0x28FF10



i.e. they are contiguous in memory with cMidd in the lowest location, followed by cName then iNo. In the 'table' (at the bottom) below I've shown what happens when you do the strcpy() call as a before and after. The table contains ADDRESS -> BEFORE -> AFTER. Note that the before and after have their hex values in brackets when they represent a character.



As you can see, what happens when you do the strcpy() is that 'university' is copied character by character from cMidd in to cName. However cName only has 4 characters (plus a nul). Once you get past that position strcpy() continues, overwriting what comes after it which, in this case, is iNo. strcpy() only stops when it reaches the nul terminator of the source string. You need to make sure that, if you're using strcpy(), the destination string is at least as long as the source string. You could consider using strncpy() instead but fundamentally you need to re-work your variable declarations for this one.



You can see from the table below that your 10 (4 bytes -> 0x0A/0x00/0x00/0x00) gets overwritten with 0x72/0x73/0x69/0x74 which, when translated to decimal (with the bytes the right way round) is 1953067890. The thing to remember here though is that this is MinGW working with 32 bit values. You have to take into account that Turbo C might location the variables slightly differently in memory, and may also be working with 16 bit ints, but the principle is the same which is, as I mentioned, that strcpy() continues copying until it finds a nul ('\0') in the source string, even if it means overwriting all sorts of other stuff in memory.



By the way - I don't know Turbo C but you could probably watch this happening using the debugger with a memory view open.



0x28FF10 -> 'u' (0x75) -> 'u' (0x75)

0x28FF11 -> 'n' (0x6E) -> 'n' (0x6E)

0x28FF12 -> 'i' (0x69) -> 'i' (0x69)

0x28FF13 -> 'v' (0x76) -> 'v' (0x76)

0x28FF14 -> 'e' (0x65) -> 'e' (0x65)

0x28FF15 -> 'r' (0x72) -> 'r' (0x72)

0x28FF16 -> 's' (0x73) -> 's' (0x73)

0x28FF17 -> 'i' (0x69) -> 'i' (0x69)

0x28FF18 -> 't' (0x74) -> 't' (0x74)

0x28FF19 -> 'y' (0x79) -> 'y' (0x79)

0x28FF1A -> '\0' (0x00) -> '\0' (0x00)

0x28FF1B -> 'e' (0x65) -> 'u' (0x75)

0x28FF1C -> 'x' (0x78) -> 'n' (0x6E)

0x28FF1D -> 'a' (0x61) -> 'i' (0x69)

0x28FF1E -> 'm' (0x6d) -> 'v' (0x76)

0x28FF1F -> '\0' (0x00) -> 'e' (0x65)

0x28FF20 -> 0x0A -> 'r' (0x72)

0x28FF21 -> 0x00 -> 's' (0x73)

0x28FF22 -> 0x00 -> 'i' (0x69)

0x28FF23 -> 0x00 -> 't' (0x74)

0x28FF24 -> 0x14 -> 'y' (0x79)

0x28FF25 -> 0x00 -> '\0' (0x00)

0x28FF26 -> 0x00 -> 0x00

0x28FF27 -> 0x00 -> 0x00

0x28FF28 -> 0x1E -> 0x1E

0x28FF29 -> 0x00 -> 0x00

0x28FF2A -> 0x00 -> 0x00

0x28FF2B -> 0x00 -> 0x00

0x28FF2C -> 0x28 -> 0x28

0x28FF2D -> 0x00 -> 0x00

0x28FF2E -> 0x00 -> 0x00

0x28FF2F -> 0x00 -> 0x00
AnalProgrammer
2011-06-20 03:10:46 UTC
I know what the problem is, I just cannot prove it.

Add the following lines of code

printf("%d\n", iNo[0]);

printf("%d\n", iNo[1]);

printf("%d\n", iNo[2]);

printf("%d\n", iNo[3]);



you will find that only the last value prints correctly.

This statement

char cName[ ] = "exam";

should create a char array of 5 elements. The last element being the"\0" end of string marker.

This statement

char cMidd[ ]= "university";

should create a char array of 11 elements. The last element being the"\0" end of string marker.



This statement

strcpy(cName,cMidd);

will copy from cMidd to cName until an end of string marker is found.



This statement

printf ("\n%s",cName);

will print cName until an end of string marker is found.



Notice that the above two statements do not use the length of the char array.

I believe that the int array is created in memory after the cName char array and what you are seeing is a corruption of the int array caused by the overflow of the strcpy move.



This is of course something you need to be aware of. C functions do not care about size where char arrays are concerned. They just move regardless.

It is up to you to make sure that all receiving char arrays are big enough to hold the largest string possible plus one byte for "\0".

If you do not do this then you will overwrite memory and corrupt other variables as you have done.



Another thing to remember is that in C you cannot find the length of an array. You have to know the length of an array. Your above problem proves this. Using the end of string marker "\0" is no guarantee of finding the length of a char array.



Have fun.
Arun
2011-06-20 02:48:41 UTC
try making int iNo[4] = {10,20,30,40};

to int iNo[3] = {10,20,30,40};
anonymous
2011-06-20 02:41:49 UTC
The code looks right. I just tried it on my machine with a different compiler and it produced the output you would expect. I'm sorry to say that I don't know what the problem is. Did you copy the code directly from your source file? Maybe it is written a little differently in your code than it is here.
Happy Days
2011-06-20 02:31:55 UTC
check with LET US C
vinodh t
2011-06-20 03:30:51 UTC
include

#include

#include

void main()

{

int iNo[4] = {10,20,30,40};

char cName[ ] = "exam";

char cMidd[ ]= "university";

clrscr();

printf("%d",iNo[0]);

strcpy(cName,cMidd);

printf ("\n%s",cName);

getch();

}







this is giving the correct answer as 10.I just replaced strcpy statement after printing that number.

It is strange for me too.I am just figuring out this.Will try to tell u shortly.





Update:::

I had figured out the problem.

the memory address is overlapping.if u give iNo[4] u will be getting the answer 10.

the problem is cName is assigned 5 bits(for storing one char in a string 1 bit of memory is needed) of memory("exam\0" along with the null character) but the cMidd contains university("university\0" contains 11 characters (11 bits).)

so once u copy that the 6 bits is needed to store university in cName.

so what happens it takes the six bits which is allocated to iNo.so 10 20 30 was not displayed because of that.iNo[3] will be give correct answer as 40.after that from iNo[4] u will be getting 10 20 30 40 correctly.

so only if u print that before strcpy u get the correct answer.



Again thank u for the question,Again i learned a lot.

if u cant understand mail me at vishares@gmail.com.

I apperciate if u send question like this.Thank u.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...