Pointer confusion in C programming language

Thread Starter

Gajyamadake

Joined Oct 9, 2019
116
Hello guys

I have difficulty to understand the pointers in C language. As far as I know a pointer is a variable whose value is the address of another variable

Code:
int student = 20;

int * pointer = &student ;
There are the two variables student and pointer.

I am declaring an integer variable student to store value 20 and I am declaring a pointer to store the address of integer variable student.

But we can do like this way also


Code:
int student = 20;

int  another_variable = &student ;
When you write program when do you think that you should use the pointer in your code ?
 

Papabravo

Joined Feb 24, 2006
12,397
Any time you have a series of identical operations on data that can be be sequentially performed using simple operations on the pointer like incrementing or decrementing.
 

WBahn

Joined Mar 31, 2012
24,692
Hello guys

I have difficulty to understand the pointers in C language. As far as I know a pointer is a variable whose value is the address of another variable

Code:
int student = 20;

int * pointer = &student ;
There are the two variables student and pointer.

I am declaring an integer variable student to store value 20 and I am declaring a pointer to store the address of integer variable student.
That is correct.

But we can do like this way also


Code:
int student = 20;

int  another_variable = &student ;
No, we can't do like this way also -- at least not if we want our code to work reliably.

At the very least, this code should throw a big warning. Ignore it at your peril.

An int and a pointer to an int may not even be the same size. Depending on the compiler and the target architecture, an int might be 16 bits, 32 bits, or 64 bits. Plus, it is interpreted as a signed quantity. A pointer is typically either 32 bits or 64 bits and it an unsigned quantity.

When you write program when do you think that you should use the pointer in your code ?
When you need or want to store the address of some object in memory.
 

bogosort

Joined Sep 24, 2011
397
When you write program when do you think that you should use the pointer in your code ?
The C language is basically a general-purpose assembly language with certain helpful abstractions and lots of syntactic sugar. One of the most helpful abstractions is the notion of a data type. In C, a variable is just a symbolic way to reference a particular memory address. If we want to associate a variable with information, we need to tell the compiler how much memory we need to store that information, and this is the primary purpose of data types: they tell the compiler how many bytes of memory to reserve for the information associated with a variable. If the programmer declares a variable as an integer, and uses the variable consistently as an integer, the compiler will quietly handle the address arithmetic behind the scenes. If, however -- whether by accident or intentionally -- the programmer uses the variable in a way that the compiler thinks is inconsistent with its declaration as an integer, then the compiler will complain, either with a warning or outright error.

Your line of code "int another_variable = &student;" is such an example. The address-of operator '&' returns a memory address, which could require 8 bytes of storage on 64-bit architectures, yet you declared 'another_variable' as an integer, which typically reserves 4 bytes of storage. To help you avoid making such a mistake, the compiler will rightfully complain.

Nonetehless, you are indeed correct that we don't need to use the pointer data type; we are free to handle the address arithmetic ourselves. Consider the following two examples, the first using the pointer data type, the second not:
C:
int v1 = 20;
int *p = &v1;
(*p)++; // increment the value that p points to
printf( "v1: %d\n", v1);
In line 3, the compiler knows that 'p' references an address that stores an integer, and so incrementing that value means incrementing the number 20. Running this would result in "v1: 21".

Here's a way to do it without the pointer data type:
C:
int v1 = 20;
size_t v2 = (size_t)&v1; // v2 is a `fake’ pointer

(*(int *)v2)++;  // equivalent to (*p)++

printf( "v1: %d\n", v1);
In line 2, we create a variable 'v2' of type 'size_t', which is guaranteed by the language to be big enough to hold a memory address. This prevents the compiler from complaining about incompatible data types; in effect, we're telling the compiler "We know what we're doing, leave us alone." The big difference comes in line 4, where we increment the value pointed to by the fake pointer. If you're very comfortable with C syntax, the statement is easy to read: cast 'v2' as a pointer to int, dereference the value, and increment. Still, the syntax is rather ugly.

Though both snippets produce the same results, the first is easier to read.

Here's another example that highlights how pointers allow the compiler to handle address arithmetic:
C:
int i;
int arr[] = {1, 2, 3};
size_t foo = (size_t)&arr; // fake pointer to first element of arr[]

for ( i = 0; i < 3; ++i )
{
  printf( "arr[%d] = %d ", i, *(int *)foo );
 
  (*(int *)foo)++; // increment the integer
 
  printf( "(+1 = %d)\n", *(int *)foo );
 
  foo = foo + sizeof(arr[i]);  // increment the fake pointer
}
Running this produces:
Code:
arr[0] = 1 (+1 = 2)
arr[1] = 2 (+1 = 3)
arr[2] = 3 (+1 = 4)
As 'arr' is an array of integers, each element of 'arr' is separated by 4 bytes -- the size of an integer. Thus, to increment the fake pointer so that it points to the next element, we have to manually add the appropriate offset (last line in the loop). Had we used an actual pointer type -- as in "int *ptr = arr;" -- then to increment the pointer we would only have to write "ptr++;" and the compiler would take care of the address arithmetic for us.

Hopefully, you can see that using pointers is more convenient for the programmer, and, perhaps more importantly, is a more robust way to program, as code that does address arithmetic by hand is brittle.
 

Thread Starter

Gajyamadake

Joined Oct 9, 2019
116
Any time you have a series of identical operations on data that can be be sequentially performed using simple operations on the pointer like incrementing or decrementing.
@Papabravo
It would nice if you had given an example to explain

That is correct.

When you need or want to store the address of some object in memory.
@WBahn
I know a pointer is a variable whose value is the address of another variable but Why we need to store the address of another variable

The C language is basically a general-purpose assembly language with certain helpful abstractions and lots of syntactic sugar. One of the most helpful abstractions is the notion of a data type. In C, a variable is just a symbolic way to reference a particular memory address. If we want to associate a variable with information, we need to tell the compiler how much memory we need to store that information, and this is the primary purpose of data types: they tell the compiler how many bytes of memory to reserve for the information associated with a variable. If the programmer declares a variable as an integer, and uses the variable consistently as an integer, the compiler will quietly handle the address arithmetic behind the scenes. If, however -- whether by accident or intentionally -- the programmer uses the variable in a way that the compiler thinks is inconsistent with its declaration as an integer, then the compiler will complain, either with a warning or outright error.
your explanations was helpful.
Hopefully, you can see that using pointers is more convenient for the programmer, and, perhaps more importantly, is a more robust way to program, as code that does address arithmetic by hand is brittle.
@bogosort
I do not want to disappointing you. I know pointer is very important but still I don't know the solid reason to use pointer. That was the reason I asked question when do you think that you should use the pointer in your code ?
 

WBahn

Joined Mar 31, 2012
24,692
@WBahn
I know a pointer is a variable whose value is the address of another variable but Why we need to store the address of another variable
Notice I mentioned an "object", not a variable.

When you instantiate an object (using a struct) you generally do so by dynamically allocating a block of memory (using a function such as malloc()), which returns the address at which that block of memory is located. Where do you think is the proper place to store that address? In a pointer variable!

Pointers are also used to pass values by reference to functions.

If you have a function fred() that takes two parameters and needs to change the values stored in both, then you would pass the addresses of the two variables to the function. Within the function you have two pointer variables so that the function has access to the actual locations where the values are stored so that it can change them.

Have you ever worked with file input/output using the FILE data type? Well, that's a pointer data type.
 

Ian Rogers

Joined Dec 12, 2012
660
Here is the most common pointer example..
C:
void PrintToScreen( char* charPtr)
   {
   while(*charPtr != NULL) 
      dataOut(*charPtr++);
   }
If you use a pointer then the whole string can be accessed by one char pointer.. ( Remembering that this pointer may be an int or even a long ) but a single variable none the less depending on the address range..
 

bogosort

Joined Sep 24, 2011
397
I do not want to disappointing you. I know pointer is very important but still I don't know the solid reason to use pointer. That was the reason I asked question when do you think that you should use the pointer in your code ?
You use a pointer any time you need an indirect reference to a location in memory. WBahn gave you two common examples when you would need such a thing. If you are new to programming and don't quite understand his examples, then I'd suggest that -- for now -- you simply accept that C has pointers and not worry too much about why they are useful. As you practice with the language and gain experience, you'll start to develop a sense of how and when to use pointers.

On the other hand, if you're already a competent programmer and are just inexperienced with references, feel free to ask for specific examples of things that pointers allow you to do that would otherwise be impossible or cumbersome.
 
Top