Question:
What will happen if we declare a pointer variable?
Big
2012-03-30 16:15:59 UTC
if i declare a pointer variable like
char *ptr;
What would be the values of ptr,*ptr,&ptr;
I know its related to random memory location. Just tell with a random example
Five answers:
?
2012-03-30 16:35:05 UTC
To answer that question, its best to use an example



Click here for an example: http://ideone.com/iUX0l



char * can act as a character array



So going off of my example



ptr is displaying the contents of the array

*ptr is displaying only the first array index of the array

&ptr is displaying the memory address of the variable
Ratchetr
2012-03-30 23:34:28 UTC
It depends on where ptr is declared.

Let's cover both cases:



char *ptr1;



int main()

{

    char *ptr2;

}



ptr1 is a global variable. The memory for it exists in your programs data segment. The values will be:

ptr1 = 0 (aka NULL). Global variables are always initialized to 0 when your program starts.



*ptr1 = ? Many systems will throw an exception if you attempt to read from memory location 0. If your system doesn't throw an exception, then you will get whatever happens to be stored at address 0 in your program.



&ptr1 = the address of the variable ptr in your data segment. I just tried it and &ptr1 happened to get an address of 0x137336c. But you would almost certainly get a different result there, and it would change as you modify your code.





ptr2 is a local variable. The memory for it exists on the stack. The values will be:

ptr2 = ? Some random value. Stack variables aren't initialized when they are declared. So their value is whatever happens to be in the memory that is used for the stack. There is no predicting what it might be.



*ptr2 = ? Since ptr itself is a random value, there is no telling what *ptr will do. On many systems, it could trhow an exception if you attempt to access it, because the random value may not be a valid memory address. If it doesn't throw an exception, you will get whatever value is stored in that random memory address. Some random number between 0 and 255.



&ptr2 = The address of the variable ptr on the stack. I just tried it and &ptr2 happened to be 0x26f844. But you would almost certainly get a different result there, and it would change as you modify your code.



I hope that was random enough for you ;-)
?
2012-03-30 23:38:53 UTC
The expression char *ptr declares a pointer to a character. Pointers work with addresses.

char a = 'a';

char *ptr = &a; // ptr now holds the address of variable a

char a2 = *ptr; // we use * to refer to the value in the pointer. a2 now 'a'

char *ptr2 = ptr; // ptr2 and ptr now both contain the address of in memory of variable a



You should consult your book for more information. This may also be helpful:

http://c-faq.com/ptrs/index.html
anonymous
2012-03-30 23:58:53 UTC
char *ptr reserves space the size of a pointer. This space has a name ("ptr"), an address, and a value (another address, since it is a pointer)



ptr returns the value of the ptr (the address it points to)

*ptr returns the value of the memory at the address in ptr's value field

&ptr is the address of the "ptr" itself (the address given when we declared "ptr")



Ex:



Suppose we have the following memory spaces for "ptr" which points to some space "test" (say an int):



name = "ptr", address = 0x16, value = 0xFF

name = "test", address = 0xFF, value = 300



ptr returns 0xFF

*ptr returns 300

&ptr returns 0x16
James Bond
2012-03-31 01:02:31 UTC
main()

{

char V; //V is a character variable and for it some memory is allocated

// For character variables one byte of memory is allocated

// Let we assume memory cell number 20120 is assigned to V by operating system



char *ptr; //ptr is a pointer variable for that also some memory is allocated

// For pointer variables 4 bytes are allocated in todays systems. May be it may soon

// change as we are having 64bit operaing systems and compilers

V='X';

//We are assigning symbol X to a character variable X such that symbol X's ASCII code

// is stored in V's memory. As V is character variable, we have assigned symbol



ptr = &V; // For pointer variables we can assign only allocated memory cell numbers.

// Here, we are assigning address of variable V to ptr.

// As ptr value is address of V, we call ptr is pointer to V. we can access V value through ptr.

// If ptr is made to point to some other variable, that can be accessed through ptr

// Now, ptr value is 20120



//NOTE: * before a pointer variables in statements other than declaration statements indicates the value in the memory pointed by that pointer variable. This * before a pointer variable is called as indirection operator.



printf("%c %c\n", V, *ptr);

Both give you X as output as ptr is pointer to V.



CHEERS


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...