Clarification on "saying" pointer declarations - c++

I've been looking a bit into pointers. I've found the small differences in declaring pointers like int* x, or int *x. However, I've always thought of *x as the actual value as the pointed memory. So when I read int *x = &a, I read it as "assign value at pointed address of x to &a", which is obviously not what is happening. The variable a mem address is being assigned to pointer x. How should I be reading it? Whenever I see *x, I just think it's the literal value at pointed address by x. Is it because in any pointer declaration, whatever value it is first initialized with it is always treated as the assigned mem address to point at?

When you see a declaration such as:
int *x = &a;
you should interpret it as:
int *x;
x = &a;
The declaration syntax is visually confusing. It is not intended that int *x = &a; have an appearance of *x = &a;. What happened historically is that we had simple assignments, like x = &a;, and we also had declarations. In the C style of declarations, we show how we want to use a variable:
int x; // I want x to be an int. Therefore, x is an int.
int *x; // I want *x to be an int. Therefore, x is a pointer to an int.
int x(); // I want x() to be an int. Therefore, x is a function that returns an int.
int x[3]; // I want x[i] to be an int. Therefore, x is an array of int.
int *x[3]; // I want *x[i] to be an int. Therefore, x is an array of pointers to int.
Next, we want to combine both the declaration and the assignment—partly for brevity in the source code, and partly to emphasize that this is the initial value for an object, the value it has the moment it is defined. To do this, the declaration and the assignment were jammed together, resulting in:
int *x = &a;
This causes *x = &a to appear in the statement, but it is not assigning &a to *x. It is initializing x, not *x. The *x appears only because it is part of the declaration. I suppose this could have been made less confusing by separating the assignment part and the declaration part but keeping them in the same statement, perhaps something like:
int *x : x = &a;
But, regardless of what could have been done, int *x = &a; is what we have.

I've found the small differences in declaring pointers like int* x, or int *x
There is no semantic difference. Both are same as well as int*x and int * x.
How should I be reading [int *x = &a]?
"x is initialised to point to a". Or alternatively, "... to store the address of a".
Furthermore, you should also read that x was declared to have the type int* i.e. pointer to int. Depending on context, different aspects of a declaration may be more important than others. For example, if we already know that a is an integer, it might be obvious from the context that a variable pointing to it is a pointer to an integer unless stated otherwise.

"Assign the address of 'a' to 'x'".
If you make an integer as such called "sum":
#include <iostream>
int main(){
int sum = 57;
std::cout << &sum << std::endl; //Prints the address
std::cout << *(&sum) << std::endl; //Prints 57
std::cout << sum << std::endl; //Prints 57
return 0;
}
You will see that the ampersand is asking for the address of the variable. Pointers are just another variable, they contain addresses instead of what we would understand as "useful values".
As far as I'm aware, it's just a question of style("int *x" vs "int* x"). When you don't declare a variable, but you invoke it, then the asterisk acts as a dereference, i.e. it steps into the address contained within the variable to go back to whatever the pointer points to.

Depending on the context, *x is pronounced very differently.
int *x;
This is pronounced: declare x to be a pointer to an int.
y = *x;
This is pronounced: take the value that x points to, and copy it to y.
Having these two entirely different pronunciations may seem strange at first. But there's a deeper meaning to it. The inventors of the C programming language wanted to allow an alternative pronunciation for the above declaration.
The alternative pronunciation for int *x; is: declare a variable such that the type of the expression *x is int.
This alternative pronunciation rule also works for very complicated types, like functions returning functions of functions, therefore it is worth learning.

I've found the small differences in declaring pointers like int* x, or int *x.
These two are the exact same, the space does not make a difference.
A pointer is just an integer that stores a memory address, like a door number which points to a house.
The ampersand in front of an existing variable is basically asking the variable what is your memory address. eg:
int var =8;
int* ptr = &var;
Here I am taking the memory address of the variable var and assigning it to a new variable called ptr.
Now I'm not 100% sure what your question is but hopefully this may help a little.
EDIT:
I would highly recommend watching this video series, It's super long but the first couple of videos explain pointers, references and operators very well, take notes if you can. The Cherno C++ Tutorial

Related

Meaning of references, address-of, dereference and pointer

Here is the way I understand * and & symbols in C and C++.
In C, * serves two purposes. First it can be used to declare a pointer variable like so int* pointerVariable
It can however be used as a dereference operator like so *pointerVariable which returns value saved at that address, it understands how to interpret bytes at that address based on what data type we have declared that pointer is pointing to. In our case int* therefore it reads bytes saved at that address and returns back whole number.
We also have address-of operator in C like so &someVariable which returns address of bytes saved underneath someVariable name.
However in C++ (not in C), we also get a possibility to use & in declaration of reference like so int& someReference. This will turn variable someReference into a reference, which means that whatever value we pass into that variable, it will automatically get address of the value we are passing into it and it will hold it.
Do I get this correctly?
Do I get this correctly?
Yes, but it is better to think about pointers and references in terms of what you want to do.
References are very useful for all those cases where you need to refer to some object without copying it. References are simple: they are always valid and there is no change in syntax when you use the object.
Pointers are for the rest of cases. Pointers allow you to work with addresses (pointer arithmetic), require explicit syntax to refer to the object behind them (*, &, -> operators), are nullable (NULL, nullptr), can be modified, etc.
In summary, references are simpler and easier to reason about. Use pointers when a reference does not cut it.
General Syntax for defining a pointer:
data-type * pointer-name = &variable-name
The data-type of the pointer must be the same as that of the variable to which it is pointing.
void type pointer can handle all data-types.
General Syntax for defining a reference variable:
data-type & reference-name = variable-name
The data-type of the reference variable must be the same as that of the variable of which it is an alias.
Let's look at each one of them, for the purpose of explanation, I will go with a simple Swap Program both in C and C++.
Swapping two variables by the pass by reference in C
#include <stdio.h>
void swap(int *,int *); //Function prototype
int main()
{
int a = 10;
int b = 20;
printf("Before Swap: a=%d, b=%d\n",a,b);
swap(&a,&b); //Value of a,b are passed by reference
printf("After Swap: a=%d, b=%d\n",a,b);
return 0;
}
void swap(int *ptra,int *ptrb)
{
int temp = *ptra;
*ptra = *ptrb;
*ptrb = temp;
}
In the code above we have declared and initialized variable a and
b to 10 and 20 respectively.
We then pass the address of a
and b to swap function by using the addressof (&) operator. This operator gives the address of the variable.
These passed arguments are assigned to the respective formal parameters which in this case are int pointers ptra and ptrb.
To swap the variables, we first need to temporarily store the value of one of the variables. For this, we stored value pointed by the pointer ptra to a variable temp. This was done by first dereferencing the pointer by using dereference (*) operator and then assigning it to temp. dereference (*) operator is used to access the value stored in the memory location pointed to by a pointer.
Once, the value of pointed by ptra is saved, we can then assign it a new value, which in this case, we assigned it the value of variable b(again with the help of dereference (*) operator). And the ptrb was assigned the value saved in temp(original value of a). Therefore, swapping the value of a and b, by altering the memory location of those variables.
Note: We can use dereference (*) operator and the addressof (&) operator together like this, *&a, they nullify each other resulting in just a
We can write a similar program in C++ by using pointers to swap two numbers as well but the language supports another type variable known as the reference variable. It provides an alias (alternative name) for a previously defined variable.
Swapping two variables by the call by reference in C++
#include <iostream>
using namespace std;
void swap(int &,int &); //Function prototype
int main()
{
int a = 10;
int b = 20;
cout << "Before Swap: a= " << a << " b= " << b << endl;
swap(a,b);
cout << "After Swap: a= " << a << " b= " << b << endl;
return 0;
}
void swap(int &refa,int &refb)
{
int temp = refa;
refa = refb;
refb = temp;
}
In the code above when we passed the variables a and b to the function swap, what happened is the variable a and b got their respective reference variables refa and refb inside the swap. It's like giving a variable another alias name.
Now, we can directly swap the variables without the dereferencing (*) operator using the reference variables.
Rest logic remains the same.
So before we get into the differences between pointers and references, I feel like we need to talk a little bit about declaration syntax, partly to explain why pointer and reference declarations are written that way and partly because the way many C++ programmers write pointer and reference declarations misrepresent that syntax (get comfortable, this is going to take a while).
In both C and C++, declarations are composed of a sequence of declaration specifiers followed by a sequence of declarators1. In a declaration like
static unsigned long int a[10], *p, f(void);
the declaration specifiers are static unsigned long int and the declarators are a[10], *p, and f(void).
Array-ness, pointer-ness, function-ness, and in C++ reference-ness are all specified as part of the declarator, not the declaration specifiers. This means when you write something like
int* p;
it’s parsed as
int (*p);
Since the unary * operator is a unique token, the compiler doesn't need whitespace to distinguish it from the int type specifier or the p identifier. You can write it as int *p;, int* p;, int * p;, or even int*p;
It also means that in a declaration like
int* p, q;
only p is declared as a pointer - q is a regular int.
The idea is that the declaration of a variable closely matches its use in the code ("declaration mimics use"). If you have a pointer to int named p and you want to access the pointed-to value, you use the * operator to dereference it:
printf( "%d\n", *p );
The expression *p has type int, so the declaration of p is written
int *p;
This tells us that the variable p has type "pointer to int" because the combination of p and the unary operator * give us an expression of type int. Most C programmers will write the pointer declaration as shown above, with the * visibly grouped with p.
Now, Bjarne and the couple of generations of C++ programmers who followed thought it was more important to emphasize the pointer-ness of p rather than the int-ness of *p, so they introduced the
int* p;
convention. However, this convention falls down for anything but a simple pointer (or pointer to pointer). It doesn't work for pointers to arrays:
int (*a)[N];
or pointers to functions
int (*f)(void);
or arrays of pointers to functions
int (*p[N])(void);
etc. Declaring an array of pointers as
int* a[N];
just indicates confused thinking. Since [] and () are postfix, you cannot associate the array-ness or function-ness with the declaration specifiers by writing
int[N] a;
int(void) f;
like you can with the unary * operator, but the unary * operator is bound to the declarator in exactly the same way as the [] and () operators are.2
C++ references break the rule about "declaration mimics use" hard. In a non-declaration statement, an expression &x always yields a pointer type. If x has type int, &x has type int *. So & has a completely different meaning in a declaration than in an expression.
So that's syntax, let's talk about pointers vs. references.
A pointer is just an address value (although with additional type information). You can do (some) arithmetic on pointers, you can initialize them to arbitrary values (or NULL), you can apply the [] subscript operator to them as though they were an array (indeed, the array subscript operation is defined in terms of pointer operations). A pointer is not required to be valid (that is, contain the address of an object during that object's lifetime) when it's first created.
A reference is another name for an object or function, not just that object's or function's address (this is why you don't use the * operator when working with references). You can't do pointer arithmetic on references, you can't assign arbitrary values to a reference, etc. When instantiated, a reference must refer to a valid object or function. How exactly references are represented internally isn't specified.
This is the C terminology - the C++ terminology is a little different.
In case it isn't clear by now I consider the T* p; idiom to be poor practice and responsible for no small amount of confusion about pointer declaration syntax; however, since that's how the C++ community has decided to do things, that's how I write my C++ code. I don't like it and it makes me itch, but it's not worth the heartburn to argue over it or to have inconsistently formatted code.
Simple answer:
Reference variables are an alias to the data passed to them, another label.
int var = 0;
int& refVar = var;
In practical terms, var and refVar are the same object.
Its worth noting that references to heap pointer data cannot deallocate (delete) the data, as its an alias of the data;
int* var = new int{0};
int& refVar = *var;
delete refVar // error
and references to the pointer itself can deallocate (delete) the data, as its an alias of the pointer.
int* var = new int{0};
int*& refVar = var;
delete refVar // good

What exactly is the purpose of the (asterisk) in pointers?

I'm new to programming and I'm trying to wrap my head around the idea of 'pointers'.
int main()
{
int x = 5;
int *pointerToInteger = & x;
cout<<pointerToInteger;
}
Why is it that when I cout << pointerToInteger; the output is a hexdecimal value, BUT when I use cout << *pointerToInteger; the output is 5 ( x=5).
* has different meaning depending on the context.
Declaration of a pointer
int* ap; // It defines ap to be a pointer to an int.
void foo(int* p); // Declares function foo.
// foo expects a pointer to an int as an argument.
Dereference a pointer in an expression.
int i = 0;
int* ap = &i; // ap points to i
*ap = 10; // Indirectly sets the value of i to 10
A multiplication operator.
int i = 10*20; // Needs no explanation.
One way to look at it, is that the variable in your source/code, say
int a=0;
Makes the 'int a' refer to a value in memory, 0. If we make a new variable, this time a (potentially smaller) "int pointer", int *, and have it point to the &a (address of a)
int*p_a=&a; //(`p_a` meaning pointer to `a` see Hungarian notation)
Hungarian notation wiki
we get p_a that points to what the value &a is. Your talking about what is at the address of a now tho, and the *p_a is a pointer to whatever is at the &a (address of a).
This has uses when you want to modify a value in memory, without creating a duplicate container.
p_a itself has a footprint in memory however (potentially smaller than a itself) and when you cout<<p_a<<endl; you will write whatever the pointer address is, not whats there. *p_a however will be &a.
p_a is normally smaller than a itself, since its just a pointer to memory and not the value itself. Does that make sense? A vector of pointers will be easier to manage than a vector of values, but they will do the same thing in many regards.
If you declare a variable of some type, then you can also declare another variable pointing to it.
For example:
int a;
int* b = &a;
So in essence, for each basic type, we also have a corresponding pointer type.
For example: short and short*.
There are two ways to "look at" variable b (that's what probably confuses most beginners):
You can consider b as a variable of type int*.
You can consider *b as a variable of type int.
Hence, some people would declare int* b, whereas others would declare int *b.
But the fact of the matter is that these two declarations are identical (the spaces are meaningless).
You can use either b as a pointer to an integer value, or *b as the actual pointed integer value.
You can get (read) the pointed value: int c = *b.
And you can set (write) the pointed value: *b = 5.
A pointer can point to any memory address, and not only to the address of some variable that you have previously declared. However, you must be careful when using pointers in order to get or set the value located at the pointed memory address.
For example:
int* a = (int*)0x8000000;
Here, we have variable a pointing to memory address 0x8000000.
If this memory address is not mapped within the memory space of your program, then any read or write operation using *a will most likely cause your program to crash, due to a memory access violation.
You can safely change the value of a, but you should be very careful changing the value of *a.
yes the asterisk * have different meanings while declaring a pointer variable and while accessing data through pointer variable. for e.g
int input = 7;
int *i_ptr = &input;/*Here * indicates that i_ptr is a pointer variable
Also address is assigned to i_ptr, not to *iptr*/
cout<<*i_ptr;/* now this * is fetch the data from assigned address */
cout<<i_ptr;/*it prints address */
for e.g if you declare like int *ptr = 7; its wrong(not an error) as pointers ptr expects valid address but you provided constant(7). upto declaration it's okay but when you go for dereferencing it like *ptr it gives problem because it doesn't know what is that data/value at 7 location. So Its always advisable to assign pointers variable with valid addresses. for e.g
int input = 7;
int *i_ptr = &input;
cout<<*i_ptr;
for example
char *ptr = "Hello"; => here * is just to inform the compiler that ptr is a pointer variable not normal one &
Hello is a char array i.e valid address, so this syntax is okay. Now you can do
if(*ptr == 'H') {
/*....*/
}
else {
/*.... */
}

When should I use * and when &? [duplicate]

This question already has answers here:
When to use references vs. pointers
(17 answers)
Closed 7 years ago.
I am learning some C++, and I came across pointers and addresses. However, in none of the materials, I could find a good explanation on when to use pointer, and when to use address. As I understand it is that when I use pointer, I POINT to address in memory, where some variable is stored. So for example:
int x = 5;
int k* = &x;
Which will mean that:
k represents x
When I change k, I also change value of x.
My question is as follows: when should I use pointer, and when should I use address? When I declare a function, should I use pointer, or address as a variable?
You have a typo there. It should be:
int x = 5;
int *k = &x;
It should be read as: k points to x. Or if you insist on the "represent" word: *k represents x.
The & operator takes any variable as argument and returns its address (pointer). The * gets a pointer (address) as argument and returns the value stored there. As such they are opposite operations: &*k is the same as k and likewise *&x is just like x. You can look at these expression this way:
x //an integer
k //pointer to integer
&x //pointer to x
*k //integer pointed to by k
*&x //integer pointed to by a pointer to x, that is x
&*k //pointer to the integer pointed to by k, that is k
Note that operator & can only be used on variables, as expressions in general do not have addresses, only variables do (well, and temporaries, but that is another matter). For example, this expression is invalid:
&(x + 1) //error!
The funniest thing with these operator is how a pointer is declared:
int *k;
You might think that it would be better written as int &k, but that's not how declarators are read in C++. Instead, that declaration should be read as:
int (*k);
that is, *k is an integer, so it results that k is a pointer to an integer.
Now, the declaration with initialization is weirder:
int *k = &x;
should actually be read as:
int (*k);
k = &x;
That is, you declare *k as being an integer, thus, k is a pointer-to-integer. And then you initialize k (the pointer) with the address of x.
Actually you can create pointers of any type, even pointers to pointers, pointers to pointers to pointers... but note that this syntax is illegal:
int x;
int **p = &&x; //error, &x is not a variable
but this is valid:
int x;
int *k = &x;
int **p = &k;
If a variable stores an address of a memory location, it is considered a pointer. So an int * for example stores the address of an int-variable. By using the dereference-operator *, you can access the memory location at the address of the pointer and assign to it.
To get the address of a variable, you use the &-operator. This is what your example does. To assign to the memory-location where x is stored, you would use the derefence-operator again like this: *k = 0;
Note that the derefence-operator and the * to express a pointer type are two different things. * and & are each other's inverse (overloading aside) operators, while int * is a type.
In C++ in particular, if the & is used together with a type, the type of the variable is a reference-type, e.g. const string &s. Just like the pointer-type this is different from the address-of operator. Reference-types do not need to be derefenced using *, but will direcly modify the memory location they reference.
Pointer operator (*) is used whenever you want to point to a variable.
Address operator (&) is used whenever you point to the memory address of a variable.

References and Pointers both store addresses?

Does int* var and &var both store addresses, the only difference is that you have to deference int* to get the value back but don't references already do that? Having trouble understanding these thoroughly.
And why is that when you have a function that accepts int* into the parameters you can pass values in by &
They are different ways of expressing what probably eventually boils down to the same thing. They are both constructs that have been invented by the language's designers, meaning your compiler's authors must implement them in whatever manner they see fit for the underlying machine.
However, just because they may represent the same thing on the machine doesn't mean that they are equivalent. Pointers allow the concept of pointing-to-nothing-ness (NULL pointers) and also allow one to perform mathematic operations to obtain a portion of memory indexed off of a starting position... like so:
int *x = new int [10];
*(x+2) = 5; //set the 3rd element of the array pointed to by 'x' to 5
is perfectly sensible.
References have no notions of such things, i.e. one can do
int *x = new int[10];
*(x+2) = 5;
int &y = *(x+2);
but not
int *x = new int[10];
*(x+2) = 5;
int &y = *(x+2);
y = y + 5;//this just changes the value of x[2]
which means it's more difficult to write off the end of a struct because of bad pointer math, so they are safer provided they've been initialized to something that makes sense (i.e. not returned from a function where they are declared on the stack or to an array element that doesn't exist)
int &dontdoit() {
//don't do this!
int x = 7;
int &y = x;
return y;
}
I think this is perfectly legal and safe in that you won't be corrupting memory, but it's not recommended as you're mixing idioms and you have no way to free the resulting allocated memory:
int &dontdothiseither() {
int *x = new int;
int &y = *x;
return y;
}
Also, you can set a pointer as many times as you like but not a reference.
int x[2];
int *y = x;//works
y = y+1; //works, now points to x[1];
int &z = x[0]; //works
z = x[1];//nope! This just sets x[0] to be the value in x[1]
int* is a variable whose value is the address of some int. &var is not a reference. The unary & operator simply returns the address of var. That's why if you have a function that takes a parameter of type int*, you use &var at the calling site.
It's a bit confusing since C++ uses & to mean both "address of" and "reference", but the context is what makes them different.
int a = 5;
int& ref = a; // Now ref and a both mean the same thing.
vs
int b = 6;
int* ptr = &b; // Now ptr POINTS to b, but they are not the same thing.
no no no!
references become nickname of that variable, where as pointers store variable's address.
suppose we have a variable of type int :
int x;
int &y=x; // now y is an other name of x
int *p=&x; // here p is a pointer, which points to x
Yes, pointers and references both store addresses, and are compiled to exactly the same code. The only major difference though is that references cannot be null, whereas pointers can - hence the well-known "null-pointer". Obviously they are accessed in different ways by the programmer: using -> and . respectively but that is really of no significance.
You can view a reference as a new "name" for a variable, while a pointer is a variable storing an address.
In practice, a reference might be implemented with pointers (so, it might store an address), but you do not need to worry about that.
For example:
int i;
int *pointer = &i; //Holds address of i
int& reference = i; //reference is a new name of i
int *pointer2 = &reference; //pointer2 holds the address of i
Yes, internally passing a pointer to T and passing a reference to T are the same in any known sensible implementation, references being semantic sugar for pointers which are always dereferenced and cannot be NULL.
Implementations are not obligated to make sense though.
After optimization, that is even sure for references used inside a function.
Still, they lead to different method / object signatures.
Also, they have different semantic load for the reader.
The & character is used for many different purposes in C++. Two of them look like they might be similar, but they're not.
The first is to create a reference:
int a = 1;
int &b = a;
Now a and b both refer to the same variable, and a change made to one will be reflected in the other.
The other usage is to create a pointer:
int *p = &a;
This has nothing to do with references at all. The & is being used as an address-of operator, taking the variable and creating a pointer that points to it.

Assigning int value to an address

I thought the following codes were correct but it is not working.
int x, *ra;
&ra = x;
and
int x, ra;
&ra = x;
Please help me if both of these code snippets are correct. If not, what errors do you see in them?
Your both expressions are incorrect, It should be:
int x, *ra;
ra = &x; // pointer variable assigning address of x
& is ampersand is an address of operator (in unary syntax), using & you can assign address of variable x into pointer variable ra.
Moreover, as your question title suggests: Assigning int value to an address.
ra is a pointer contains address of variable x so you can assign a new value to x via ra
*ra = 20;
Here * before pointer variable (in unary syntax) is deference operator gives value at the address.
Because you have also tagged question to c++ so I think you are confuse with reference variable declaration, that is:
int x = 10;
int &ra = x; // reference at time of declaration
Accordingly in case of the reference variable, if you want to assign a new value to x it is very simply in syntax as we do with value variable:
ra = 20;
(notice even ra is reference variable we assign to x without & or * still change reflects, this is the benefit of reference variable: simple to use capable as pointers!)
Remember reference binding given at the time of declaration and it can't change where pointer variable can point to the new variable later in the program.
In C we only have pointer and value variables, whereas in C++ we have a pointer, reference and value variables. In my linked answer I tried to explain differences between pointer and reference variable.
Both are incorrect.
When you declare a pointer, you assign it the address of a variable. You are attempting the other way round. The correct way would be:
int x,*ra;
ra = &x;
Both of those causes, in the way you have them in your question, undefined behavior.
For the first, you don't initialize the pointer, meaning it points to a random location (or NULL if the variable is global).
For the second, you try to change the address the variable is located at, which (if it even would compile) is not allowed.
Here's some annotated code:
int main () {
// declare an int variable
int x = 0;
// declare a pointer to an int variable
int *p;
// get the memory address of `x`
// using the address-of operator
&x;
// let `p` point to `x` by assigning the address of `x` to `p`
p = &x;
// assign `x` a value directly
x = 42;
// assign `x` a value indirectly via `p`
// using the dereference operator
*p = 0;
// get the value of `x` directly
x;
// get the value of `x` indirectly via `p`
// using the dereference operator
*p;
}
Note that dereferencing a pointer that doesn't point to a valid object of the specified type is not allowed.
So you normally shouldn't do things like the following (unless you really know what you are doing):
*(int*)(12345) = 42; // assign an integer value to an arbitrary memory address
Here is my 2 cent.
If you are going onto understanding pointer in C. First make the distinction between * the operator and * the type qualifier/specifier.
See that in C * is a syntaxique element that can plays both role but never at the same time. A type qualifier:
int a;
int * c = &a;
int * my_function_returning_pointer();
And for getting the proper int. As an operator. ( *c is an alias of a)
*c = 9;
I admit that is quite confusing and can trap a lot of beginner. Make sure that you recognize when * is used as an operator or when it is used as a type qualifier.
The same things apply to & although it is less often used as type qualifier.
int & f = returning_a_reference();
int my_function( int & refParam);
It is more often use for getting the address of an object. Thus it is used as an operator.
c = &f;
case 1:
int x,*ra;
&ra = x;
it is wrong in c, because in c we can point to a memory location by using a pointer ( i.e *ra in your case ). this can be done as fallows
int x, *ra; x ---------
ra=&x; ra --->1000 | value |
----------
NOTE : with out initializing a variable we can't use pointer to hold the address of that variable, so you better to first initialize variable, then set pointer to that memory location.
int x, *ra;
x=7;
ra=&x;
Fallowing may be helpful to you:
problems(mistake we do ) in handling pointers :
1.)
int a ,*p;
p=a; // assigning value instead of address. that leads to segmentation fault at run time.
2)
int a, *p;
&p=a; // look at here wrong assignment
3)
char *p="srinivas"
strcat(p, "helo" ) ; // we cant add a substring to the constant string.
4) int a=5, *p;
p=5; // the pointer here will points to location 5 in the memory, that may damage whole system, be care full in these type of assignments.