mstac
07-12-2007, 02:27 AM
Hi all,
I am quite a newbie to macosxhints forum, so I hope I'm in the right category, and my post is relevant.
I'm having problem with some very basic C code, and I really don't understand why.
this code gives me a EXC_BAD_ACCESS:
#include <stdio.h>
int main (int argc, const char * argv[]) {
int* a; int* b;
*a =10;
printf("Hello, World!\n");
return 0;
}
The problem comes when I add the int*b, ie this works
#include <stdio.h>
int main (int argc, const char * argv[]) {
int* a;
*a =10;
printf("Hello, World!\n");
return 0;
}
Actually, even this works:
#include <stdio.h>
int main (int argc, const char * argv[]) {
int* a;int* b;
printf("Hello, World!\n");
return 0;
}
From the debugger, it looks like when I creat a second int pointer, the first one gets the address "0x0", in which I can't write. If I had 3 pointers, the first 2 ones would get this "0x0" address... declaring the second pointer after I assigned a value to *a does not change anything.
I'm very confused,it looks like I can only use one integer pointer in my program... Please help me!
thank you,
Martin
PS: It's not a compiling error, everything compiles well. It's just when I execute my code, I get a EXC_BAD_ACCESS error.
PS2: I'm using Xcode on MacOS 10.4.10.
I am quite a newbie to macosxhints forum, so I hope I'm in the right category, and my post is relevant.
I'm having problem with some very basic C code, and I really don't understand why.
this code gives me a EXC_BAD_ACCESS:
#include <stdio.h>
int main (int argc, const char * argv[]) {
int* a; int* b;
*a =10;
printf("Hello, World!\n");
return 0;
}
The problem comes when I add the int*b, ie this works
#include <stdio.h>
int main (int argc, const char * argv[]) {
int* a;
*a =10;
printf("Hello, World!\n");
return 0;
}
Actually, even this works:
#include <stdio.h>
int main (int argc, const char * argv[]) {
int* a;int* b;
printf("Hello, World!\n");
return 0;
}
From the debugger, it looks like when I creat a second int pointer, the first one gets the address "0x0", in which I can't write. If I had 3 pointers, the first 2 ones would get this "0x0" address... declaring the second pointer after I assigned a value to *a does not change anything.
I'm very confused,it looks like I can only use one integer pointer in my program... Please help me!
thank you,
Martin
PS: It's not a compiling error, everything compiles well. It's just when I execute my code, I get a EXC_BAD_ACCESS error.
PS2: I'm using Xcode on MacOS 10.4.10.