SITERAW

On variables and pointers in C

Author Message
Locust # Posted two hours ago
Locust

Hi everyone,

I'm currently working through the C programming tutorial. I've reached the infamous chapter on pointers and I have to admit it, a few parts have kind of tripped me up.

Here's the code that my questions are based on:

#include 

void foo(int *a)
{
*a =
145; // We change the value at the address pointed to by a
printf("Foo: Variable a = %d\n", *a); // *a == 145
}

int main(void)
{
int a;

a = 10;
printf("Main: Variable a = %d\n", a); // a == 10
foo(&a);
// We pass the address of a
printf("Main: Variable a = %d\n", a); // a == 145
return (0);
}

Here are my questions:

  • In the line foo(&a);, does the & indeed give the address of the variable a?
  • What's the purpose of the * in void foo(int *a)?
  • What does int mean in int main(void)?
  • And finally, is the void in int main(void) strictly necessary?

Thanks in advance!

Ads
Rel0ad # Posted one hour ago
Rel0ad

Hey there,

  • Yep, the & operator gives you the address of the variable that follows it.
  • The * is used to access the value that the pointer is pointing to — in other words, it dereferences the pointer. If you just wrote a = 145; inside foo(), you'd be changing the pointer itself, not the value it points to. But by writing *a = 145;, you're modifying the value at the address stored in a.
  • A C program returns a value to the operating system when it finishes running — that return value can be used by whatever called the program (like a shell script, for instance).
  • No, void isn't strictly required anymore. But I'd still recommend including it when your function takes no parameters — it makes things clearer for both the compiler and anyone reading your code.

Edit: To dig a bit deeper into point 3, here's a basic example:

int main(void) {
return 1; // try return 0 too
}

Now if you call x.exe from a batch script y.bat:

@echo off
x
if errorlevel 1 goto err
echo x ran fine
goto eob
:err
echo x returned an error code: %errorlevel%
:eob

Keklord # Posted one hour ago

Just to add to the answer above:

  • When you use * in a function parameter like int *a, it means the function is expecting a pointer. And when you use * in a variable declaration, it means you're declaring a pointer.

As for why you'd use a pointer in a parameter: C uses pass-by-value, which means function parameters are read-only copies by default. But by passing a pointer, you can actually modify the original variable from inside the function.

Also, the * is what lets you dereference the pointer — to access or modify the value at the given memory address — like in *a = 145;.

  • So, since the function expects a pointer, you pass the address of a using the & operator: foo(&a);.
  • Regarding main's return value, the stdlib.h library defines two constants you can use: EXIT_SUCCESS and EXIT_FAILURE.
  • The void in main(void) just tells the compiler that this function takes no arguments. It also prevents someone from accidentally calling main() with parameters.
  • That said, the real, standard signature of main is:

int main(int argc, char* argv[])

— which lets your program accept command-line arguments when it's run.

Locust # Posted 7 minutes ago
Locust

Thanks for your (clear) answers. It's tough figuring out all these little nuances when you're learning on your own. That's why forums like this are so helpful :D

Have a great weekend, everyone.

Post a reply

Please be kind and courteous in your replies. Don't roast others for no reason.