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

Help Me To Debug This Prog!!!!

momotaro
357 100+
when I give the prog the first data it crushs !!!! and give me this message:
Unhandled exception at 0x004114c5 in CallocMalloc.exe: 0xC0000005: Access violation reading location 0x00000002.



Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2.  
  3. typedef struct
  4. {
  5.     char name[20];
  6.     double gpa;
  7. }student;
  8.  
  9. void fill_up( int *stud_num, student *stud_arr);
  10. void display(student stud_arr[]);
  11.  
  12. student *stud_arr;
  13. int *stud_num;
  14. void main()
  15. {
  16.     int stud_num;
  17.     fill_up(&stud_num, stud_arr);
  18.     display(stud_arr);
  19.     while(1);
  20. }
  21.  
  22. void fill_up(int *stud_num, student *stud_arr)
  23. {
  24.     int i;
  25.     printf("How many student --> "); scanf("%d", &stud_num);
  26.     stud_arr = (student *)calloc(*stud_num, sizeof(student));
  27.     for(i = 1; i <= *stud_num; i++)
  28.     {
  29.         printf("student %d --> ", i); scanf("%s", stud_arr[i].name);
  30.         printf("gpa --> "); scanf("%lf", &stud_arr[i].gpa);
  31.     }
  32. }
  33.  
  34. void display(student *stud_arr)
  35. {
  36.     int j;
  37.     for(j = 1; j <= *stud_num; j++)
  38.     {
  39.         printf("student %d --> %s", j, stud_arr[j].name);
  40.         printf("gpa --> %.2f\n\n", stud_arr[j].gpa);
  41.     }
  42. }
Feb 16 '07 #1
4 1415
Hi ,

You made two mistakes in the program.

1. You a didnt allocate memory to stud_num.
2. while(1) ; ---- > this statement means an infinite loop, so control will never come out of this line.

Change these, it will work.


-Ajai omtri
Feb 17 '07 #2
momotaro
357 100+
Sorry but i think that u have not a good level in c!
:)
first we DON'T allocate memory to int variables son!
and the while(1) it is to keep the window open after the program has ended!
Feb 17 '07 #3
Ganon11
3,652 Expert 2GB
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2.  
  3. typedef struct
  4. {
  5.     char name[20];
  6.     double gpa;
  7. }student;
  8.  
  9. void fill_up( int *stud_num, student *stud_arr);
  10. void display(student stud_arr[]);
  11.  
  12. student *stud_arr;
  13. int *stud_num;
  14. void main()
  15. {
  16.     int stud_num;
  17.     fill_up(&stud_num, stud_arr);
  18.     display(stud_arr);
  19.     while(1);
  20. }
  21.  
  22. void fill_up(int *stud_num, student *stud_arr)
  23. {
  24.     int i;
  25.     printf("How many student --> "); scanf("%d", &stud_num);
  26.     stud_arr = (student *)calloc(*stud_num, sizeof(student));
  27.     for(i = 1; i <= *stud_num; i++)
  28.     {
  29.         printf("student %d --> ", i); scanf("%s", stud_arr[i].name);
  30.         printf("gpa --> "); scanf("%lf", &stud_arr[i].gpa);
  31.     }
  32. }
  33.  
  34. void display(student *stud_arr)
  35. {
  36.     int j;
  37.     for(j = 1; j <= *stud_num; j++)
  38.     {
  39.         printf("student %d --> %s", j, stud_arr[j].name);
  40.         printf("gpa --> %.2f\n\n", stud_arr[j].gpa);
  41.     }
  42. }
How about this:

1) You declare int *stud_num as a global variable, but then redefine it in your main function. I think you should get rid of the global variables, as you pass variables to each method that needs them.

2) When calling fill_up, you pass stud_num's address, and it is held in a temporary pointer value. This is OK, but then you use scanf to input into the address of the pointer. Shouldn't you write "scanf("%d", stud_num);" so that data is written to the value held in the stud_num pointer i.e. the integer variable declared in main()?

3) Also in fill_up, you have a for...loop running from 1 to <= *stud_num, then use it to set values of your array from 1, 2, 3...*stud_num. But indexing starts at 0, and you will be writing to an out-of-bounds area at stud_arr[*stud_num], so your loop should be from 0 to < *stud_num.

4) The same mistake as (3) occurs in display() - you will be trying to write data at an out-of-bounds place.

Finally, while your while(1); loop will create the 'hanging' effect you desire, it also makes closing the program a hassle, and closing it with some sort of shortcut (not sure if you're using Windows, so I can't say using the X button) will probably create some slight errors - not completely sure what, but if a program doesn't close successfully, it may create a problem in the processor.

I suggest displaying a simple message (such as "Press any key and enter to continue"), and then using scanf to input into a temporary variable. This will stop the program from flashing off the screen, but you will still be able to enter that value and end the program as it was meant to end.
Feb 17 '07 #4
momotaro
357 100+
thx It sounds better!
Feb 17 '07 #5

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

Similar topics

5
by: codymanix | last post by:
What are the Differences between Debug & Release? Is the byte code the same and there is only difference in JIT compilation? And is there a great impact to Performance? In c++, there where...
6
by: Cormac | last post by:
Hi everyone, I'm writing Pure Data externals in C++ using Ms Visual C++ 7.0 and Windows2000. I'm running motions sensor hardware so there's a bit of real time data processing involved. When...
4
by: David. E. Goble | last post by:
#include <stdio.h> #include <string.h> #define FALSE 0 #define TRUE 1 #define LINESIZE 255 int setupoutfile(char filename, FILE *outfile) { strcat(filename, ".js");
10
by: lindmark.daniel | last post by:
Since I'm a mac-head, I don't have the possibility to make this little script I wrote work proper on a MSIE browser. I know there's smarts in here that easily could spot my f**k ups and tell me...
1
by: apacheutara | last post by:
hai.. i juz started learning C++ language & started to try creating a program. its a simple contacts list. here's the code: ========================================================= ...
3
by: svasanth | last post by:
hi.. I am a newbee to c++ programming.. the program compiles.. but when i run, the command window prompt hangs.. and there is no output.. Pls help me out.. the source code is attached.. thanks...
3
by: jack 1 | last post by:
how towrite a complete Java application using a one-dimensional array to keep track of the toys in a shop has in its warehouse. It can enter or remove items from the inventory list, change the...
3
by: poojagupta1984 | last post by:
What i know is: In java, all byte and short data types are automatically promoted to int during a calculation. And, if you are casting a large data type like int to a byte then the int is reduced...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.