2

C beginner. Below is a page from the lecture notes at my university (4 hours total to learn structured programming). The procedure manages to change the places that global pointers address, despite not having any output. This is confusing as hell, and seems a dangerous thing to be allowed. Can anyone explain what is happening in the second case, and how it happens. Ie: how are the normal rules bypassed?

Pointers as input arguments slide

I know this has been asked from a "I want to use this" background here and less generally in other places. But I am asking why this is allowed, when normally, changing values in a function would produce no effect unless they are returned?

Community
  • 1
  • 1
geeeeeeek
  • 33
  • 2
  • 4
    hmm... i have a picture in my computer , if i copy it to a flash drive and give you.you can edit it.But my copy remains unchanged.That is what happens when you pass by value.(first case). In second case i give a link to my picture(lets say you can access over the LAN or smtn) When you use the link(address) to open the image and edit it, the change is made on the original .that is pass by reference (second case). – c_mnon Feb 16 '16 at 23:10
  • If this wasn't allowed then how would you write the swap function? – M.M Feb 16 '16 at 23:11
  • 1
    The normal rules re not bypassed: the second swap function does not modify `a` or `b`. Instead it modifies `*a` and `*b` (completely different) – M.M Feb 16 '16 at 23:11
  • 1
    I like the internet link analogy, thanks! I know that it not working would result in alot of functions breaking, so I understand why it's needed, I suppose it's the step from being told that functions **never** change any values except their outputs to the step up, where this function can reach into a location and change data under certain circumstances. – geeeeeeek Feb 16 '16 at 23:25
  • So to understand this. The pointers a and b are the inputs. And so the contents of their memory location, is frozen like normal (by some means), but the places they point to aren't? So when we change the values in the memory locations *a and *b, that's why we are allowed to do this? Otherwise C wouldn't work. This makes sense. I'm going to think of it this way. Please tell me if this is the equivalent of running over Dennis Ritchie's dog. – geeeeeeek Feb 16 '16 at 23:40
  • In 2nd `swap()` example, `a` is a copy of `&x`. Had code changed `a`, the pointer, it would not affect `x` nor `&x`. Instead code changes `*a`. When `swap()` is done and returned, then `x` has a new value. The `&x` is still the same. – chux - Reinstate Monica Feb 16 '16 at 23:46

1 Answers1

2

That is basically the whole purpose of having pointers: you can change the values contained at a location in memory, and you can share the same pointer between different pieces of code.

In C, you can make a pointer to almost anything, and then pass that pointer to another function so that the other function can modify the object the pointer points to. Whew, that's a mouthful. C and C++ are fairly unusual in letting you make pointers so freely, many other languages have tight restrictions on what pointers can point to. Java, for example, only allows pointers to point to object instances, and you can't do pointer arithmetic. (Yes, Java has pointers.)

And yes, it is "dangerous", in the sense that it's easy to write incorrect programs that misuse pointers. Dangling pointers, buffer overflows, null pointer dereferencing, and many other types of programming errors are possible in C because of the way pointers work. If you're lucky, your program will crash and you can debug it. If you're unlucky, it won't crash.

The danger in using pointers is one of the primary motivations between other languages such as Java, things like std::unique_ptr<> in C++, C#, Rust, Go, etc. If you are writing C, you just have to be careful. So, why use C? Sometimes, you need to use those pointers and scribble over memory however you want. The Linux kernel is mostly written in C, and plenty of language runtimes are at least partially written in C (I know this applies at least to Python, Java, and Go).

People still write in assembly language, too. Sometimes even C doesn't let you do what you need.

Dietrich Epp
  • 205,541
  • 37
  • 345
  • 415