1

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.

0

5 Answers 5

7

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
Sign up to request clarification or add additional context in comments.

4 Comments

So the assignement "int &a=x;" is not supported by C. correct me if i am wrong.
"int & a = x" declares a reference - references are part of C++, not C.
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
You might want to consider switching to the MinGW compiler and use the Code::Blocks IDE - see codeblocks.org
2

You have declared c and d as int pointers and you are trying to update their pointed values, they aren't pointing to anywhere valid since you never initialize them with valid memory.

Try c = &x; and then play with c.

Comments

2

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;

2 Comments

Or point them to some valid memory e.g. c = &x (set c so it points to the same memory as x, just as "int &a=x" sets a to point to the same memory as x)
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);
2

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.

Comments

0

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;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.