Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do certain C/C++ functions use pointers as parameters?

Tags:

c++

c

pointers

I'm coming from C# and I'm learning C++ (with this tutorial) so I have a relatively insubstantial amount of knowledge on memory, but the only use I see in pointers is "saving space" and iterating through arrays. So why do functions like the scanf function take pointers as parameters? For instance:

printf("What's your name?: "); and then: scanf("%s",&userName). Wouldn't it make more sense to just pass the variable itself as an argument? The C book I was looking at said that using the variable itself would produce unexpected results, but I don't see why. Could anyone please enlighten me in a C++ fashion? I switched from learning C because I realized how much I love OOP.

like image 585
airplaneman19 Avatar asked Aug 14 '11 02:08

airplaneman19


4 Answers

There are 3 different ways of passing a variable to a function in C++, pass by copy, pass by reference and pass by pointer.

#include <iostream>

void passByCopy(int a)
{
   a += 1;
}

void passByReference(int &a)
{
   a += 1;
}

void passByPointer(int *a)
{
   (*a) += 1; // De-reference then increment.
}

int main()
{
   int a = 0;
   // Passing by copy, creates a copy of the 'a' object, then sends it to the function.
   passByCopy(a);
   std::cout << a << std::endl; // Outputs 0

   // Passing by reference, causes the 'a' object in the function to reference the 'a'
   // object at this scope. The value of 'a' will change. 
   passByReference(a);
   std::cout << a << std::endl; // Outputs 1

   // Passing by pointer, does almost the same thing as a pass by reference, except a 
   // pointer value can by NULL, while a reference can't.
   passByPointer(&a);

   std::cout << a << std::endl; // Outputs 2
}

With scanf, the purpose of the function is to pass values to variables in the current scope, so it can't use pass by copy. It doesn't use pass by reference for two reasons, one is that it is an old C function, so pass by reference didn't exist when it was written. The second is that it is a variadic function, which means that internally the function receives a list of pointers rather than a series of arguments.

like image 78
Darcy Rayner Avatar answered Oct 13 '22 16:10

Darcy Rayner


C and C++ pass variables by value; the formal parameter is a different object in memory from the actual parameter. Thus, if the formal parameter is modified in the function, the value of the actual parameter isn't changed.

By passing a pointer, the function can modify the contents of the actual parameter:

void swap(int *a, int *b)
{
  int t = *a;
  *a = *b;
  *b = t;
}

...
swap(&x, &y);

Thus, writing to *a in swap is equivalent to writing to x in the caller.

Pointers in C serve three main purposes:

  • Faking pass-by-reference semantics, as above (as Karl notes in the comments, C++ has a mechanism that supports true pass-by-reference);
  • Tracking dynamically-allocated memory (the memory allocation functions malloc, calloc, and realloc and the C++ new operator return pointer values);
  • Building dynamic data structures (trees, lists, queues, stacks, etc.).
like image 45
John Bode Avatar answered Oct 13 '22 15:10

John Bode


All parameters are passed by value in C. If you want to pass by reference, you need to explicitly pass the address of the variable in question. Passing by reference is important when you want the function to modify the variable passed in.

C++ has references, but since C++ code often calls C library functions, you still see the same syntax used in C++ sometimes.

like image 42
Caleb Avatar answered Oct 13 '22 15:10

Caleb


In your specific example, scanf is part of the C standard library. C does not have a string class, so strings are represented as arrays of characters.

In C, the address of the first character is usually passed when strings are required. This can either be &str[0] or str. I'm not sure it's correct to have &username if it's expecting a char *, I guess it depends on username.

like image 35
Cade Roux Avatar answered Oct 13 '22 15:10

Cade Roux