This program is supposed to take in a three digit number and change it into its palindrome. 123 would become 321.
The logic is correct, and the program compiles correctly. 🙂 However, the logic of these does not come easily.
My prof explains things with “stack diagrams” and I find them to be helpful. I created this program based off another program because I noticed the similarities between this and a different program I made, but how does the pointing work?
#include <stdio.h>
void reverse_number(int in_val, int *out_val) {
int ones, tens, hundreds;
ones = in_val % 10;
tens = (in_val % 100 - ones) / 10;
hundreds = (in_val - (ones + tens)) / 100;
*out_val = (ones * 100) + (tens * 10) + hundreds;
}
int main() {
int in_val;
int out_val;
printf("Give a three digit num to reverse: \n");
scanf("%d", &in_val);
reverse_number(in_val, &out_val);
printf("New number is: %d \n", out_val);
return 0;
}
Also, I am now beginning to understand how to write programs based on a kind of template with these pointers, and I understand very basically what the star inside a parameter means (declared as a pointer variable).
For example, I know that m = &q; gives variable m the address of another variable q and I know that m = *g; would mean that the value at the address g would go into m but I am really unfamiliar with how these work in the context of a function and a main file.
If someone could lay out the fundamental logic of how it would work (in this program) that would be awesome. As a math major, I can understand the operations of the math and stuff but the pointers have me not confused but it just seems to me that there are ways to do it without needing to deal with the address of a variable, etc.
Well, since you understood
&and*operators perfectly, the rest is very very simple.Let’s say you have:
Then if you say:
m2will contain the same value asm, that is, it will have the address ofq. Therefore,*mand*m2will give you the same value (which is the value ofq) (you do understand that*is the inverse operator of&right? So*(&q) = qand&(*m) = m(in the later case,mneeds to be a pointer for*to be applicable.))So, how does this work with functions? Simple! When you pass arguments to functions, you pass them by value. When you pass by pointer, you are actually passing by value, the pointer of the variable.
So let’s examine your function call in detail:
I renamed your
in_valandout_valin the main toin_origandout_origso it won’t get mixed with those ofreverse_number.Now,
&out_origis the address ofout_orig. When passed as arguments, this gets copied intoout_valargument ofreverse_number. This is exactly like writing:Now, if you had the above line in your
main, you could just write*out_val = something;and it would changeout_orig, right? Well, since you have the address ofout_originout_val, then who cares if*out_valis being set inmainorreverse_number?So you see? When you have a pointer, you can just copy it around, whether by copying it to another variable or passing it as argument of a function (which is basically the same thing), you can still access the same variable it is pointing to. After all, all the copies have the same value: address of
out_orig. Now if you want to access it in a function or inmain, it doesn’t really matter.Edit:
*in pointer definition*can be used to define a pointer too and this has nothing to do with the previous usage of*as an operator that gets the value of an address.This is just definition, so you have to learn it:
If you have a value of type
type(for exampleint), then the address of that variable (usingoperator &) has typetype *(in this exampleint *). Since a pointer takes that address, the type of the pointer istype *.On the contrary, if a pointer has type
type *(for exampleint *), then getting the value where the pointer points to (usingoperator *) has typetype(in this exampleint).In summary, you can say something like this:
So let’s see some examples:
If you noticed, I said that
&adds one*to the type of variable, but*removes one*from the type of expression. Why is that? Because&gives the address of a variable. Of course, because nothing else has an address. For examplea+b(possibly) doesn’t have any address in the memory, and if it does, it’s just temporary and useless.operator *however, works on addresses. No matter how you compute the address,operator *works on it. Examples:In case of a dynamic 2d array: