Why do we use & before ptr->points?

  • Context: MHB 
  • Thread starter Thread starter evinda
  • Start date Start date
  • Tags Tags
    Pointers
Click For Summary
SUMMARY

The discussion clarifies the use of the address-of operator (&) in C programming when dealing with pointers and arrays. Specifically, when using scanf to input data, the operator is necessary for non-array variables like ptr->points, which is a floating-point number. In contrast, when dealing with arrays, such as ptr->id, ptr->firstname, and ptr->lastname, the array name itself acts as a pointer to its first element, making the use of & redundant. This distinction is crucial for correctly passing memory addresses to functions.

PREREQUISITES
  • Understanding of C programming language
  • Familiarity with pointers and memory management
  • Knowledge of the scanf function
  • Basic concepts of arrays in C
NEXT STEPS
  • Study the differences between pointers and arrays in C
  • Learn about memory allocation and deallocation in C
  • Explore advanced usage of scanf and printf functions
  • Investigate common pitfalls in pointer arithmetic
USEFUL FOR

C programmers, software developers, and students learning about memory management and input handling in C.

evinda
Gold Member
MHB
Messages
3,741
Reaction score
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
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.
 
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)
 

Similar threads

Replies
7
Views
2K
  • · Replies 118 ·
4
Replies
118
Views
10K
Replies
7
Views
5K
  • · Replies 19 ·
Replies
19
Views
4K
Replies
4
Views
3K
  • · Replies 2 ·
Replies
2
Views
11K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 12 ·
Replies
12
Views
3K
Replies
47
Views
5K
  • · Replies 3 ·
Replies
3
Views
1K