Why do we use & before ptr->points?

  • MHB
  • Thread starter evinda
  • Start date
  • Tags
    Pointers
In summary, the conversation discusses the use of the "address of" operator (&) in C programming when dealing with arrays and non-array data types. The "pointer" concept is also mentioned, with an explanation that it refers to a memory location that contains the memory location of a specific data. It is explained that when using scanf, we need to provide a memory address, which is why we use the & operator. However, when dealing with arrays, the array name is treated as the same memory address as the first element in the array, so using & is not necessary. Finally, it is stated that we use & before ptr->points because it is not an array, but a floating point number, and we need to get its memory
  • #1
evinda
Gold Member
MHB
3,836
0
Hello! (Wave)

I have a question... 🧐

https://dyclassroom.com/c/c-passing-structure-pointer-to-function

At the Complete code stated at the site above, at this part:

Code:
for (i = 0; i < 3; i++) {
    printf("Enter detail of student #%d\n", (i + 1));
    printf("Enter ID: ");
    scanf("%s", ptr->id);
    printf("Enter first name: ");
    scanf("%s", ptr->firstname);
    printf("Enter last name: ");
    scanf("%s", ptr->lastname);
    printf("Enter Points: ");
    scanf("%f", &ptr->points);
    
    // update pointer to point at next element
    // of the array std
    ptr++;
  }
why when the input is an array we do not use & and when the input is not array we use it?

So why we use only & before ptr->points?

Which is the difference? :oops::unsure:
 
Technology news on Phys.org
  • #2
First this can vary with the compute language. Are you referring to C++?

Do you understand what a "pointer" is?

A computer has many "memory locations" in which it can store numbers. A pointer (to x) is, technically, a memory location which contains the memory location in which "x" is stored. Putting & before a pointer changes it to the memory location in which "x" is stored. More specifically, if I have the number "7" stored somewhere in memory, the pointer "P" is the memory location in which "7" is stored while "&P" is the number "7" itself.
 
  • #3
evinda said:
why when the input is an array we do not use & and when the input is not array we use it?

Which is the difference?

First off, we need to provide a memory address to scanf.
Generally, we do that by putting & in front of the variable that we want to fill. (Nerd)

If the data member is an array, there is actually no difference whether we put an & in front of it or not.
Suppose we have char arr[10];.
Then the C standard says that arr is treated as the same memory address as &arr[0].
If we use &arr, we are saying we want to have the memory address of the array, which is the same thing.
And we can't take the address of a memory address - that does not make sense. (Nerd)

evinda said:
So why we use only & before ptr->points?

ptr->points is not an array. Instead it is a floating point number.
So we have to put & in front of it to get its memory address. (Nerd)
 

FAQ: Why do we use & before ptr->points?

When do we use & at pointers?

Generally, the & symbol is used at pointers to retrieve the memory address of a variable. This is useful when passing variables by reference in functions or when using pointers to manipulate data directly.

Can you give an example of using & at pointers?

Sure. Let's say we have a variable called "num" with a value of 5. If we declare a pointer variable "ptr" and set it equal to &num, then ptr will now contain the memory address of the variable num. Any changes made to the value at that address will also change the value of num.

Do we always need to use & when working with pointers?

No, there are cases where we do not need to use the & symbol when working with pointers. For example, when declaring a pointer variable and assigning it a memory address, the & symbol is not needed. Also, when passing an array to a function, the name of the array acts as a pointer to its first element, so & is not needed.

Are there any other uses for & at pointers?

Yes, the & symbol can also be used to create pointers to pointers. This is useful when working with multi-dimensional arrays or when passing a pointer variable by reference in a function.

Can you explain the difference between * and & when used with pointers?

The * symbol is used to declare a pointer variable, while the & symbol is used to retrieve the memory address of a variable. Essentially, * is used to create a pointer and & is used to access the value stored at a memory address.

Similar threads

Back
Top