Some question about pointer in c

Thread Starter

khatus

Joined Jul 2, 2018
84
Hello guys i have some question regarding C pointer.



Here What is meant by mixed call here?

Having trouble understanding the meaning of the red lines? Can anyone explain?
 

Papabravo

Joined Feb 24, 2006
13,688
The variables area and perimeter are declared in main() and are local to that function. This means that a function like areaperi() would not normally be able to see or access those variables. In this case, the &-operator was used to create pointers to those variables. It was granting the function areaperi() the ability to modify the values of the variables area and perimeter. This is an ability that the function would not normally have. So from inside the function areaperi(), using the *-operator we can modify the value of whatever the pointer is pointing at.

This is actually a very dangerous thing to do. In a simple program like this it is not so bad. Imagine a piece of software with 50,000 lines of code and suddenly somebody is modifying values they should not be able to modify. How are you going to find the culprit? When you pass a pointer to a function, nothing prevents the function from modifying the pointer and changeing something else. Problem is the function may have no clear idea what the something else is or belongs to. Do you see the problem? It all depends on how the storage is allocated and we cannot know that at compile time.
 

WBahn

Joined Mar 31, 2012
25,731
The stated what they meant by "mixed call" -- passing a mixture of values (for radius) and addresses (for area and perimeter).

As for the meaning of the underscored text, it's pretty straightforward. Main has three variables, radius, perimeter, and area, that are stored at three locations in memory.

For the sake of discussion, let's say that those addresses are at 100, 101, and 102 respectively. I'll indicate the address where a variable is stored by putting it in curly braces after the variable name. Now let's say that the values stored in those variable are 5 (because of the user input) in radius and 92.4 and -1273.1 in the other two (due to the random garbage in memory in the uninitialized variables).

radius{100} = 5
area{101} = 92.4
perimeter{102} = -1273.1

Now we call the function with radius (the value stored in the variable) and &area (the address of the variable and &perimeter (again, the address of the variable). So the actual values passed are

areaperi(5, 101, 102)

Within the function there are three variables, r, a, and p. Those are each stored at some memory location. Let's use 200, 201, and 202. Since they are all parameters to the function, the arguments (the values passed to the function) get copied into them as part of the function calling process. So we have:

r{200} = 5
a{201} = 101
p{202} = 102

Now we execute

*a = 3.14 * r * r;

We evaluate the expression on the right and get 78.5.

The "*a = " means to take the value at the right and store it at the address pointed to by the variable 'a'. In other words, take the value stored in 'a' and use that as the address where the value gets written. So in our case we have

*{101} = 78.5

So the value in memory location 101 is change from 92.4 to 78.5.

A similar thing happens with the next statement and the value in memory location 102 gets changed from -1273.1 to 31.4.

Now the function returns back to main() and the memory looks like:

radius{100} = 5
area{101} = 78.5
perimeter{102} = 31.4

Even if we had changed the value 'r' in the function, that would have no effect on the value of 'radius' in main() because they are stored in two different memory locations.

But when we changed *a and *p in the function, we change in value in the actual memory locations that variable 'area' and 'perimeter' in main() are stored, so when main() resumes, it "sees" those changes.
 
Top