The main difference here is where you are storing the value "20000000".
In the first example, p is located in addresses belonging to "main" memory space (the stack presumably, beacuse "main" is the first function called upon start). You are passing the memory address of p (where p is located) to the function "change_value". The function assumes the memory address you are passing it's valid (meaning that it can be written safely). If you call "change_value" with null or with an invalid address (an address outside of all segments) you will cause a segfault. In the second example, "change_value" creates a new memory space (in the heap) to store "20000000". This means you aren't "changing" anything. Every time you call "change_value" it creates a new """instance""" of "20000000". Plus, if you don't free p before calling it again ("change_value(&p)"), you will be trashing memory (memory that cannot be released later, because you don't know their addresses). On Mon, Mar 28, 2016 at 3:50 AM, Edward Bartolo <edb...@gmail.com> wrote: > Hi, > > As the title of the email indicates, I am doing some exercises to make > sense out of C pointer syntax. I have been using pointers for as long > as I have been programming without issues, apart from the usual > initial programmatic errors when new code is run for the first time. > However, C pointer syntax is proving to be as unintuitive as it can > be. For this reason, I am doing some exercises regarding C pointer > use. > > I am attaching two short C programs that I created and which I tested > to work although the mechanism by which they work is still somewhat > hazy to me. Both programs use a function to change the value of a > parameter. I want to understand, as opposed to knowing by rote, the > mechanism why they work. Please note that I didn't consult any books > to create the pointers. This is because I have already the concepts, > but I cannot make sense, as in deeply understanding the details, of > pointer syntax as used in C. > > Edward > > _______________________________________________ > Dng mailing list > Dng@lists.dyne.org > https://mailinglists.dyne.org/cgi-bin/mailman/listinfo/dng > >
_______________________________________________ Dng mailing list Dng@lists.dyne.org https://mailinglists.dyne.org/cgi-bin/mailman/listinfo/dng