Error in writing array to file

Posted 17 January 2013 - 04:35 AM

Hello and thanks for your time. I came across an unusual problem, while executing the following code. A[0][0], when written onto the text becomes a 0, while it should have been a 1. All other numbers are as they should be.

Re: Error in writing array to file

Posted 17 January 2013 - 09:24 AM

Do you believe you were born with an understanding of scanf? Please read this page carefully and answer the following questions:
1. Which type of data does a "%d" format specifier tell scanf to write to?
2. What is the type of &a in your code?
3. What will scanf return when it encounters a match error? For example, consider when scanf expects to see a series of decimal digit characters, but is instead given 'a'.
4. Will the 'a' from the above example remain in stdin?
5. If the 'a' from the example does remain in stdin, what happens next time you use scanf("%d", ...)?
6. What will scanf return when it encounters EOF (eg. when you press CTRL+Z on Windows), or some other input error?
7. Let us assume you tell scanf to put two values into two variables. What should scanf return, assuming that the values were successfully put into their variables?

I would suggest using getchar from stdio.h instead of getch from conio.h, for portability reasons. C can be dangerous for people to learn by "trial and error", or by "example". Our time is better put to use when clarifying concepts for people who have already read books and manuals. I prefer not to quote from books for people who are too lazy to "learn by reading". In addition, you'll get a much more comprehensive "learn by reading" from a book than you will by asking this forum to debug your code for you.

ps. In case you're too lazy to read the manual for getchar, it returns an int. If you're sensible, that's the kind of data type you'll use for your "ans" variable...

pps. Go out and find a book! "The C Programming Language" by Kernighan & Ritchie will be okay. Do the exercises...

Re: Error in writing array to file

You declared A to have a size of 9 so trying to acces element 9 is accessng your array out of bounds. Remember arrays in C start at zero and end at size - 1.

Jim

I am not accessing element [9][9], i am using a function which wipes out the entire array. It does not work in any other way i tried and i am not sure if it should.

undefined behaviour, on 17 January 2013 - 09:24 AM, said:

-Lots of stuff here, read above-

I have carefully read and understood 50-80% of the page you have posted. However at this point, i am pretty certain that you are way out of point. You see, the problem does not arise when scanning, but rather when printing. There is no "scanf" in case 2, however, this is the case that produces the problem. It is obvious that i am not asking for debugging, as i have already found a way around the problem. I am asking out of sheer curiosity, hoping that someone may take the time to figure out what causes this error, so we may all learn our bit (including whoever may stumble on this page).

You automatically assumed that i am a lazy student who has read no books or manuals and was asking for desperate help to get through a class he does not deserve to pass. Sorry, i am not. I am using a book and i am doing the excercises, that's what got me here! I am not asking you to teach me something, nor do i desire it. I am asking you if you can figure what the problem is. That's all.

Re: Error in writing array to file

I am not accessing element [9][9], i am using a function which wipes out the entire array. It does not work in any other way i tried and i am not sure if it should.

This:

A[9][9]=Clean(A);

You are trying to assign a value to A[9][9], notice the equal sign, which is out of bounds for your array, which is not allowed. Please show your Clean() function.

Quote

You see, the problem does not arise when scanning, but rather when printing. There is no "scanf" in case 2,

Part of what he is saying is that you used the "%d" specifier in your scanf() on line 14 of your main() snippet. This specifier is used for an int, but a is defined as a short. There is a difference between the short and an int. You should be using a "%h" specifier for this short. When you use the wrong specifier with scanf you can get many undefined problems. Always use the correct format specifier with both the scanf() and printf() families of functions.

By the way why are you using a short instead of the int? With todays desktop systems the benifits of using a short are usually outweighed by the negatives.

a has been changed to int, without any output difference. What negatives could using a short int have? Can you elaborate a bit? I did not know they had any differences other than size and the choice was based solely on reducing memory consumption (yes, by those few bits />)

Sorry for not using code tags and btw how could i edit? It's usually easy for me to find that button but for some reason i can't spot it... />

Re: Error in writing array to file

Posted 18 January 2013 - 07:41 PM

If you don't want to be called a lazy student, then answer my questions for yourself, rather than waiting for the others to point out that your problem was addressed in my questions. Otherwise, there is no sense in getting upset about being a lazy student. I'm still waiting for an answer to the questions...