views:

233

answers:

6

I looking for a clarification regarding the pointers. I have compiled the following code in bordland c++ 5.5.1 without any errors. But while i am trying to execute gives a core error.

int main ()
{
    int x=10,y=20;

     int &a=x;
     int &b=y;

     int *c;
     int *d;

     *c=x;
     *d=y;

return 0;
}

Basically I am trying to create two reference variable (a,b) and assigned with two variables (x,y). after that I created two pointer variable(c,d) and tried to assign same variables (x,y). This gives me error while exection and not at compilation.

whether I am doing any wrong, this is not a standard assignments of pointer variable. why the pointer assignment is getting failed at this point. Please help me to understand this.

1st Update:

Thanks to all. First, I understood that I am working on a C++ feature (reference variable). Second, I need to allocate memory for the pointer variables before play with it.

+7  A: 

The code you posted is C++, not C. And your problem is that you need to make those pointers actually point at something:

int * c = & x;   // make c point at x
* c = 42;        // changes x
anon
So the assignement "int " is not supported by C. correct me if i am wrong.
chinnagaja
anon
Thanks Neil, the problem is, I am using cpp compiler and by default the files are stored with cpp extention so all c++ features are supported at compilation
chinnagaja
You might want to consider switching to the MinGW compiler and use the Code::Blocks IDE - see http://www.codeblocks.org
anon
A: 

You have defined two pointer variables c and d but you have not allocated any memory for them. You need to assign some memory to both of them.

Do this:

int *c = &x;
int *d = &y;
Aamir
+2  A: 
Arkaitz Jimenez
A: 

dude you are saying *c=x .. when did you assign some memory to c ?

Rohin
+2  A: 

You are dereferencing an invalid pointer. 'c' and 'd' were not assigned a memory location and so will be using whatever was previously in memory as their location. You need to do:

int *c = new int;
int *d = new int;
Nat Ryall
Binary Worrier
If you assign memory to them in this way, make sure you don't forget to clear the memory when you're are done with them. Use: delete c; delete d;Also the way to do this in C: int *c = (int *)malloc(sizeof(int)); And when done: free(c);
Daniel Bingham
+2  A: 

It looks to me like you're not entirely clear on the syntax.

Typing int *c is tricky, because it looks like you're declaring an integer with the name *c. However, this is not the case. This will declare a pointer to an integer, and in C/C++, pointers to a certain type are denoted by appending a * after the type name. Thus, you're declaring a variable of type int* (pointer to an integer), with the name c, even though the spacing makes it look different. For the record, typing int* c yields the same result, and is arguably more readable.

It follows then that typing *c does not reference your variable, as it is actually called c. Instead, what this does is dereference the pointer: it returns whatever object the pointer is pointing to. Or, if the pointer is invalid, cause an error.

If you want to declare a pointer and set it to point to an object at a later point, you need to do this:

int *c;

// stuff goes here

c = &x;

&x will return x's address in memory, which can be assigned to a pointer as a value. Then, you can manipulate the value through the pointer by dereferencing it: e.g. *c = 15.

suszterpatt