Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
November 15, 2020 11:54 pm GMT

Do you know what a pointer is?

Simply explaining a pointer

A pointer is a variable that contains the address of another variable. Basically saying that a pointer is a variable that points to the memory location of another variable and can change the value at that address indirectly.

Making sense of value and memory address

Two facts we know or have to know to set things straight:

  • & for the address and
  • * for the value at an address.

Let's say we have int num = 3; We assign the value 3 to the variable num.

So to print out the value of num:

printf("value of num: %d
", num);/* value of num: 3 */
Enter fullscreen mode Exit fullscreen mode

How do we then get the address of num? Remember the &? We precede the variable with &.

printf("address of num: %p
", &num);/* address of num: 0x7ffe5faa05cc */
Enter fullscreen mode Exit fullscreen mode

So now we have an address, how do we then get the value at that address? Remember the second fact, about *? Precede the memory address with a *. We got the address by &num and by preceding the address, &num, by *, as *(&num), we would get the value at that address.

printf("value at address of num: %d
", *(&num));/* value at address of num: 3 */
Enter fullscreen mode Exit fullscreen mode

Takeaways

So what we have been able to deduce is:

  • int num = 3; means num has a value of 3.
  • &num returns the memory address of num.
  • *(&num) returns the value at the memory address, &num, of num.

Final script

#include <stdio.h>int main() {    int num = 3;    printf("value of num: %d
", num); printf("address of num: %p
", &num); printf("value at address of num: %d
", *(&num)); return 0;}
Enter fullscreen mode Exit fullscreen mode

Delving into pointers

Creating a pointer is simple. It is basically as declaring or initializing a variable - well a pointer is a variable. Consider the snippet below as to how to create a pointer.

// NOTE: int is used for the sake of simplicity// case 1 - initialize a pointerint *ptr = &var;// case 2 - declare and define later// ptr should be initialized to NULL// until it is assigned a valid location// int *ptr1;int *ptr1 = NULL;ptr = &var;
Enter fullscreen mode Exit fullscreen mode

Now let's get down to pointer business. Say we have num assigned the value of 3, just as we have done before. We shall create a pointer to num.

int num = 3;int *ptr = &num;
Enter fullscreen mode Exit fullscreen mode

I have a question for you? You better play with the code to understand better.

Question

Which is the pointer? *, ptr or *ptr?

My answer

Well, I hope you gave it some thought.

  • * is not the pointer. Remember the second fact. Given an address, * returns that value at that memory address.
  • *ptr is the value at the memory address, &num - num.As such is not the pointer.
  • ptr is the pointer. This is so because it has the memory address of num as value.

I will encourage you to take a break, clear your head and read again or proceed.

Pointer value and address

How do we get the value of a pointer? Don't get confused about the value of a pointer and what value the pointer points to. Try to understand the two before moving on. Something more or less like, the value of a pointer and the value at a pointer - what the pointer points to.

printf("value of ptr: %p
", ptr);/* value of ptr: 0x7ffe5faa05cc */
Enter fullscreen mode Exit fullscreen mode

How do we get the value at the pointer? Now the pointer, ptr is the same as &num. As such the value at the pointer (memory address) - &num, is *(&num). *ptr == *(&num).

printf("value at ptr: %d
", *ptr);/* value at ptr: 3 */
Enter fullscreen mode Exit fullscreen mode

The pointer, ptr, has its own address. How do we get the address of a pointer? This is a familiar question. Precede the variable with &.

printf("address of ptr: %p
", &ptr);/* address of ptr: 0x7ffe5faa05d0 */
Enter fullscreen mode Exit fullscreen mode

Take aways

  • In int *ptr = &num, ptr is the pointer.
  • ptr should be assigned NULL until given a location.
  • ptr is the same as &num as such *ptr, *(&num) is the same as num.
  • The pointer has its own address, &ptr.

Final script

#include <stdio.h>int main() {    int num = 3;    printf("value of num: %d
", num); printf("address of num: %p
", &num); printf("value at address of num: %d
", *(&num)); int *ptr = &num; printf("value of ptr: %p
", ptr); printf("value at ptr: %d
", *ptr); printf("address of ptr: %p
", &ptr); return 0;}
Enter fullscreen mode Exit fullscreen mode

Source


Original Link: https://dev.to/otumianempire/do-you-know-what-a-pointer-is-38l1

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To