-2

Are function parameters real variables or are they variables that store another variable or what even are they exactly at this point. Pointer parameters are included too. If you put & in the argument then why do you even need to declare pointer parameter in the function if you already got the memory address with &? Does the pointer parameter actually store memory address afterall or not?

PuuRaidur
  • 3
  • 2
  • 1
    What do you mean by "real variable"? – Dai Jul 03 '22 at 19:17
  • 'Are function parameters real variables' sure. You can mutate them, eg. you can increment an int parameter just fine. Try it and see:) – Martin James Jul 03 '22 at 19:19
  • @MartinJames [unless it's `const` tho](https://softwareengineering.stackexchange.com/a/204720/91916) – Dai Jul 03 '22 at 19:21
  • @Dai sure. I never use const, (it just introduces more problems than it solves), so I forgot about that one:) – Martin James Jul 03 '22 at 19:23
  • 2
    @MartinJames Curious, I'm the opposite: I'm a huge fan: `const` has helped me avoid countless issues (e.g. relating to arguments that should not be mutable) - _declaring intent_ is important when writing self-documenting code. – Dai Jul 03 '22 at 19:24
  • 1
    @MartinJames: That's a very bad suggestion and absolutely not the case. What you consider to be "problems" are more likely issues with your coding style. Please consider removing that comment. – einpoklum Jul 03 '22 at 19:28
  • @einpoklum I made no suggestion, so I cannot withdraw it. Feel free to use const qualifiers if it works for you. I am happier with eliminating annoying type mismatches and/or avoidable casts in projects with many translation units. YMMV:) – Martin James Jul 03 '22 at 19:33
  • 1
    @MartinJames: You are misguiding OP with the suggestion that using `const` "introduces problems". It does not. – einpoklum Jul 03 '22 at 19:34

2 Answers2

1

You may want to watch a video explaining how pointer parameters to functions work: https://www.youtube.com/watch?v=LW8Rfh6TzGg

But let me answer your specific questions...

Are function parameters real variables

Yes. But their scope is the body of the function; and their lifetimes are during invocations of the function: They come into existence when the function begins execution, and cease to exist when you return from the function.

or are they variables that store another variable

Variables can't store variables. Variables store values. (Pointer variables' values happen to be addresses.)

Pointer parameters are included too. If you put & in the argument then why do you even need to declare pointer parameter in the function if you already got the memory address with &?

Suppose you have a variable int x. Now, x is an integer, but &x is a pointer-to-an-integer - the address of the x variable.

In C, you can't define a parameter and have it represent a different variable elsewhere. What you can do is a pass an address of an external variable, then go through the address to read or write the value of that variable.

Does the pointer parameter actually store memory address after all or not?

It does actually store an address.

einpoklum
  • 118,144
  • 57
  • 340
  • 684
0

Are function parameters real variables

Yes. Inside a function, a parameter acts just like a local variable (that was initialized to the passed-in value at the top of the function).

i.e.

void foo(int a)
{
   [...]
}

is logically equivalent to this (pseudocode):

void foo(...)
{
   int a = /* the value that the caller called foo() with */
   [...]
}

If you put & in the argument then why do you even need to declare pointer parameter in the function if you already got the memory address with &?

Placing & in the argument allows you to pass a pointer to the value, rather than passing the value itself.

If you want to pass a pointer, then your function needs to accept a pointer type as its argument, otherwise your call to the function won't compile.

Does the pointer parameter actually store memory address afterall or not?

Yes, it does.

Jeremy Friesner
  • 70,199
  • 15
  • 131
  • 234
  • so regular parameters are just a copy of the argument, but pointer parameter actually stores a value(memory address)? Also, how do things work if you pass a pointer as a argument. What about the parameters then and are the changes made to the variable to what the pointer as a argument points to? – PuuRaidur Jul 03 '22 at 21:13
  • Pointer parameters are copies of the argument as well; but the argument is a pointer. What it points to is a separate thing, unrelated to parameter-passing. – Jeremy Friesner Jul 04 '22 at 02:06
  • wdym unrelated to parameter-passing, you are passing a memory address, aren't you? – PuuRaidur Jul 04 '22 at 13:50
  • I mean that a pointer-argument is passed by value, in exactly the same way as any other argument. The parameter-passing behavior is the same as for passing an int or a float or anything else, only the type is different. – Jeremy Friesner Jul 04 '22 at 14:19
  • why in functions you always have to use asterisk next to pointer variable? In main function you don't have to do it. Why is it that way in other functions? – PuuRaidur Jul 04 '22 at 14:24
  • The asterisk is used to dereference a pointer -- i.e. to pass the value that the pointer points to, rather than the pointer itself. I think you need to find a good C book and read it, as this is the sort of thing a C book would tell you. https://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list – Jeremy Friesner Jul 04 '22 at 21:41