![]() |
| | #91 | |
| Kernel hacker Join Date: Jul 2007 Location: Farncombe, Surrey, England
Posts: 15,686
| Quote:
-- Mats
__________________ Compilers can produce warnings - make the compiler programmers happy: Use them! Please don't PM me for help - and no, I don't do help over instant messengers. | |
| matsp is offline | |
| | #92 |
| Confused College Student Join Date: Jan 2009
Posts: 62
| Here's the code of the running program: Code: #include <stdlib.h>
#include <stdio.h>
#define big 200
struct integer {
int* digits;
int size;
};
struct integer* read_integer(char* stringInt);
void print(struct integer *p);
struct integer* add(struct integer *p, struct integer *q);
struct integer* subtract(struct integer *p, struct integer *q);
int compare(struct integer *p, struct integer *q);
int main(){
FILE *ifp;
char numbr1[big];
char numbr2[big];
struct integer *num1;
struct integer *num2;
struct integer *num3;
int stringnum;
int i;
int j;
int k;
int comp;
int intarray[big];
int option;
//Read in the file
ifp = fopen("bigint.txt", "r");
//Scan for the number of strings
fscanf(ifp, "%d", &stringnum);
//Read in the numbers
for (k=0; k<stringnum; k++){
fscanf(ifp, "%d, %s, %s", option, numbr1, numbr2);
printf("%d\n", option);
num1 = read_integer(numbr1);
num2 = read_integer(numbr2);
if(option==1){
num3 = add(num1, num2);
}
else if(option==2){
num3 = subtract(num1, num2);
}
else if(option==3){
comp = compare(num1, num2);
}
}
//Read the array
printf("The arrays are: \n");
system("PAUSE");
return 0;
};
//Define functions
struct integer* read_integer(char* stringInt){
int i;
struct integer *read;
read = (struct integer*)malloc(sizeof(struct integer));
read->size = strlen(stringInt);
read->digits = (int*)(malloc(read->size*sizeof(int)));
//Move the integer into the array
int x = read->size - 1;
for(i = read->size - 1; i < 0; i++){
read->digits[i]=(int)(stringInt[x] - '0');
printf("%d", read->digits[i]);
x--;
}
return read;
}
struct integer* add(struct integer *p, struct integer *q){
}
struct integer* subtract(struct integer *p, struct integer *q){
}
int compare(struct integer *p, struct integer *q){
int i;
if(p->size < q->size){
printf("Integer 2 is greater than Integer 1");
return -1;
}
else if(p->size < q->size){
printf("Integer 1 is greater than Integer 2");
return 1;
}
else if(p->size = q->size){
for(i = 0; i > p->size; i++){
if(*q[i].digits > *p[i].digits){
printf("Integer 2 is greater than Integer 1");
return -1;
}
else if(*q[i].digits < *p[i].digits){
printf("Integer 1 is greater than Integer 2");
return 1;
}
}
return 0;
}
}
|
| Graham Aker is offline | |
| | #93 |
| Kernel hacker Join Date: Jul 2007 Location: Farncombe, Surrey, England
Posts: 15,686
| Does your compiler allow you to enable warnings? gcc -Wall gives this: Code: number.c: In function `main': number.c:40: warning: format argument is not a pointer (arg 3) number.c:25: warning: unused variable `i' number.c:26: warning: unused variable `j' number.c:29: warning: unused variable `intarray' number.c: In function `read_integer': number.c:65: warning: implicit declaration of function `strlen' number.c: In function `add': number.c:76: warning: control reaches end of non-void function number.c: In function `subtract': number.c:79: warning: control reaches end of non-void function number.c: In function `compare': number.c:92: warning: suggest parentheses around assignment used as truth value number.c:105: warning: control reaches end of non-void function Code: if(p->size < q->size){
printf("Integer 2 is greater than Integer 1");
return -1;
}
else if(p->size < q->size){
printf("Integer 1 is greater than Integer 2");
return 1;
}
else if(p->size = q->size){
Also, I doubt you get past your fscanf() line. -- Mats
__________________ Compilers can produce warnings - make the compiler programmers happy: Use them! Please don't PM me for help - and no, I don't do help over instant messengers. |
| matsp is offline | |
| | #94 | |
| Confused College Student Join Date: Jan 2009
Posts: 62
| Quote:
And what do you mean you doubt I get past my fscanf() line? EDIT: Is that running with the input file I gave earlier? | |
| Graham Aker is offline | |
| | #95 | |
| Kernel hacker Join Date: Jul 2007 Location: Farncombe, Surrey, England
Posts: 15,686
| Quote:
And my compiled code crashes inside fscanf() when trying the input file you gave earlier in the thread - with two 27-or-so digit numbers, one starting with 74 and the second starting with 29. Not that what input file is given matters, as the fault is in the call to fscanf, not in the rest of the code. Also, the warning for line 40 is the one we are talking about - I missed mentioning it in the "I get these warnings" post, but it's definitely an important one. -- Mats
__________________ Compilers can produce warnings - make the compiler programmers happy: Use them! Please don't PM me for help - and no, I don't do help over instant messengers. | |
| matsp is offline | |
| | #96 | |
| Confused College Student Join Date: Jan 2009
Posts: 62
| Quote:
| |
| Graham Aker is offline | |
| | #97 |
| Kernel hacker Join Date: Jul 2007 Location: Farncombe, Surrey, England
Posts: 15,686
| Well, unless you posted a different version of the code than you are actually running, it is VERY LIKELY that you will crash on the line of fscanf(), but it is POSSIBLE that you don't under some circumstances - because it is what is called "undefined behaviour". Particularly since "option" is not initialized, your runtime environment may end up with a different value than I do - try setting option to zero before reading it - that ought to crash it. However, I also checked the return value from fscanf(), and each fscanf() reads ONE element, not the three you expect. That's probably caused by the commas in the format string, which are not matched by commas in the input file - unless you are using a different input file than what I have: Code: 2 7432968527294356297648975290 2986783975917945029875942976 -- Mats
__________________ Compilers can produce warnings - make the compiler programmers happy: Use them! Please don't PM me for help - and no, I don't do help over instant messengers. |
| matsp is offline | |
| | #98 | |
| Confused College Student Join Date: Jan 2009
Posts: 62
| Quote:
Code: 3 1 8888888888 2222222222 2 9999999999 10000000000 2 10000000000 9999999999 | |
| Graham Aker is offline | |
| | #99 |
| In my head Join Date: Dec 2008 Location: In my head
Posts: 328
| Hi Graham. Someone else has started a thread for a problem which appears similar to yours. ![]() struct pointer
__________________ M$ rules! *Turns towards Redmond and bows* Last edited by happyclown; 01-26-2009 at 05:14 PM. Reason: changed some words |
| happyclown is offline | |
| | #100 | |
| Confused College Student Join Date: Jan 2009
Posts: 62
| Quote:
I'll definitely need to take a look at that... EDIT: Lucky guy, he is. He didn't have to do the extra options... Last edited by Graham Aker; 01-26-2009 at 05:17 PM. | |
| Graham Aker is offline | |
| | #101 |
| Confused College Student Join Date: Jan 2009
Posts: 62
| Aright, I've done a little more work on the program, and it's running, but Option, numbr1, and numbr2 are spitting junk back at me. Can anyone help me fix this? Code: #include <stdlib.h>
#include <stdio.h>
#define big 200
struct integer {
int* digits;
int size;
};
struct integer* read_integer(char* stringInt);
void print(struct integer *p);
struct integer* add(struct integer *p, struct integer *q);
struct integer* subtract(struct integer *p, struct integer *q);
int compare(struct integer *p, struct integer *q);
int main(){
FILE *ifp;
char numbr1[big];
char numbr2[big];
struct integer *num1;
struct integer *num2;
struct integer *num3;
int stringnum;
int i;
int j;
int k;
int comp;
int intarray[big];
int option = 0;
//Read in the file
ifp = fopen("bigint.txt", "r");
//Scan for the number of strings
fscanf(ifp, "%d", &stringnum);
printf("%d\n", stringnum);
//Read in the numbers
for (k=0; k<stringnum; k++){
printf("%d\n", k);
fscanf(ifp, "%d, %s, %s", &option, numbr1, numbr2);
printf("%d, %s, %s\n", option, numbr1, numbr2);
num1 = read_integer(numbr1);
num2 = read_integer(numbr2);
if(option==1){
num3 = add(num1, num2);
}
else if(option==2){
num3 = subtract(num1, num2);
}
else if(option==3){
comp = compare(num1, num2);
}
}
//Read the array
printf("The arrays are: \n");
system("PAUSE");
return 0;
};
//Define functions
struct integer* read_integer(char* stringInt){
int i;
struct integer *read;
read = (struct integer*)malloc(sizeof(struct integer));
read->size = strlen(stringInt);
read->digits = (int*)(malloc(read->size*sizeof(int)));
//Move the integer into the array
int x = read->size - 1;
for(i = read->size - 1; i < 0; i++){
read->digits[i]=(int)(stringInt[x] - '0');
printf("%d", read->digits[i]);
x--;
}
return read;
}
void print(struct integer *p){
int i;
printf("%d\n", p->size);
//this print loop prints out wrong even though the other one is right
for(i = p->size -1 ; i >= 0; i--){
printf("%d\n", p->digits[i]);
}
}
struct integer* add(struct integer *p, struct integer *q){
}
struct integer* subtract(struct integer *p, struct integer *q){
}
int compare(struct integer *p, struct integer *q){
int i;
if(p->size < q->size){
printf("Integer 2 is greater than Integer 1");
return -1;
}
else if(p->size < q->size){
printf("Integer 1 is greater than Integer 2");
return 1;
}
else {
for(i = 0; i > p->size; i++){
if(*q[i].digits > *p[i].digits){
printf("Integer 2 is greater than Integer 1");
return -1;
}
else if(*q[i].digits < *p[i].digits){
printf("Integer 1 is greater than Integer 2");
return 1;
}
}
return 0;
}
}
|
| Graham Aker is offline | |
![]() |
| Tags |
| access violation, array, code, segmentation fault |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Istream::Release access violation? [C++] | A10 | Windows Programming | 10 | 01-13-2009 10:56 PM |
| Access violation... can't figure it out... | Raigne | C++ Programming | 7 | 10-11-2007 10:52 AM |
| access violation in int array | George2 | C Programming | 2 | 08-02-2007 11:28 PM |
| FtpFileFind access violation with MS VC++ 6.0 | earth_angel | C++ Programming | 3 | 09-22-2005 07:02 PM |
| 0xC0000005: Access Violation | Strider | Windows Programming | 3 | 11-07-2001 02:46 PM |