I’m learning C and I’m still not sure if I understood the difference between &
and *
yet.
Allow me to try to explain it:
int a; // Declares a variable int *b; // Declares a pointer int &c; // Not possible a = 10; b = &a; // b gets the address of a *b = 20; // a now has the value 20
I got these, but then it becomes confusing.
void funct(int a) // A declaration of a function, a is declared void funct(int *a) // a is declared as a pointer void funct(int &a) // a now receives only pointers (address) funct(a) // Creates a copy of a funct(*a) // Uses a pointer, can create a pointer of a pointer in some cases funct(&a) // Sends an address of a pointer
So, both funct(*a)
and funct(&a)
are correct, right? What’s the difference?
Related Posts:
- What does “dereferencing” a pointer mean?
- What does “dereferencing” a pointer mean?
- What is the difference between const int*, const int * const, and int const *?
- What is the difference between const int*, const int * const, and int const *?
- How to make an array with a dynamic size? General usage of dynamic arrays (maybe pointers too)?
- Is the sizeof(some pointer) always equal to four?
- Typedef function pointer?
- What is the use of intptr_t?
- Why use pointers?
- Warning: comparison of distinct pointer types
- Difference between the int * i and int** i
- How many spaces for tab character(\t)?
- What does (~0L) mean?
- What are the differences between a pointer variable and a reference variable in C++?
- What is the difference between float and double?
- What is the effect of extern “C” in C++?
- Why are #ifndef and #define used in C++ header files?
- What exactly is the difference between “pass by reference” in C and in C++?
- When to use extern “C” in simple words? [duplicate]
- What is a char*?
- What exactly is nullptr?
- Are the C++ & and * operators inverses in all contexts?
- Why use conio.h?
- Convert an int to ASCII character
- Why unsigned int 0xFFFFFFFF is equal to int -1?
- What is the significance of return 0 in C and C++?
- What is a dangling pointer?
- What does `*&` in a function declaration mean?
- (->) arrow operator and (.) dot operator , class pointer
- What is use of c_str function In c++
- What is uintptr_t data type
- What is the C version of RMI
- C++ – No matching member function for call to ‘push_back’
- What is uintptr_t data type
- What does int & mean
- What is the printf format specifier for bool?
- Fastest way to check if a file exist using standard C++/C++11,14,17/C?
- Is there a function to copy an array in C/C++?
- Difference between using Makefile and CMake to compile the code
- Why does the arrow (->) operator in C exist?
- What is the difference between a static and const variable?
- Should I learn C before learning C++?
- Eclipse C++ : “Program “g++” not found in PATH”
- Visual Studio debugger error: Unable to start program Specified file cannot be found
- Passing a 2D array to a C++ function
- unsigned int vs. size_t
- Fatal error: iostream: No such file or directory in compiling C program using GCC
- error: expected primary-expression before ‘)’ token (C)
- Examples of good gotos in C or C++
- Can’t resolve Error: indirection requires pointer operand (‘int’ invalid)
- What is activation record in the context of C and C++?
- How to create a dynamically-allocated array of const objects, but have values assigned to them?
- Fastest way to check if a file exist using standard C++/C++11,14,17/C?
- How to convert C++ Code to C
- Fatal error: ‘stdafx.h’ file not found
- What does int & mean
- How to use glOrtho() in OpenGL?
- Sorting Linked List C++ with pointers
- Delete 2D array C++
- Best C/C++ Network Library
- 2D array vs array of arrays
- How do malloc() and free() work?
- C++ Expression must have pointer-to-object type
- C++ strings and pointers
- How to initialize a vector of pointers
- How can I get the list of files in a directory using C or C++?
- Debug vs Release in CMake
- How can I get the list of files in a directory using C or C++?
- Multi-character constant warnings
- What is the C equivalent to the C++ cin statement?
- C++ – Assigning null to a std::string
- What are the different versions of exec used for in C and C++?
- What’s the difference between nexti and stepi in gdb?
- C++ – Too Many Initializers for Arrays
- Reading string by char till end of line C/C++
- C++ – include unistd.h: why not cunistd?
- error: invalid initialization of non-const reference of type ‘int&’ from an rvalue of type ‘int’
- gcc: undefined reference to
- Reference to non-static member function must be called
- What does ** mean in C?
- C++ pass an array by reference
- How to write log base(2) in c/c++
- Function stoi not declared
- What is the source of the data for the ProgramFiles, ProgramW6432Dir, ProgramFilesDir (x86), CommonProgramFiles environment variables?
- What is the job of autogen.sh when building a c++ package on Linux
- What’s the Use of ‘\r’ escape sequence?
- C++ Array of pointers: delete or delete []?
- Why should I use a pointer rather than the object itself?
- Function stoi not declared
- Using OpenMP with clang
- cc1.exe System Error – libwinpthread-1.dll missing – But it isn’t
- Difference between function arguments declared with & and * in C++
- Is C++ Array passed by reference or by pointer?
- Linker error: “linker input file unused because linking not done”, undefined reference to a function in that file
- set head to NULL (‘NULL’ : undeclared identifier)
- What is a `char*`?
- Whats the difference between UInt8 and uint8_t
- C++ Swapping Pointers
- The tilde operator in C
- Differences between unique_ptr and shared_ptr