answersLogoWhite

0

What is the advantage of using pointer in c?

Updated: 8/21/2019
User Avatar

Wiki User

6y ago

Best Answer

Pointer variables have many uses. Primarily, we use pointers to keep track of variables allocated on the heap (the free store). We cannot name these variables because they do not exist at compile time, so we must use pointer variables to store the addresses of these variables as and when they are allocated at runtime. Hence the return value of the C standard library malloc() function is a memory address.

We also need to use pointers when passing arrays to and from functions because arrays cannot be passed by value, they can only be passed by reference. More generally, we can use pointers to pass and return any variable by reference, particularly large structures that would be too expensive to copy if passed by value.

In C++ we can use native reference types rather than pointers, but in C there is no native reference type, so we must use pointers if we wish to work with memory addresses. We often use the term reference when referring to pointers in C, however there are important distinctions. Unlike a pointer, a reference is not a variable (it is an alias or alternate name for a memory address) and references can never be null. The null address (usually address 0) is a system address which we can assign to a pointer whenever we have no valid address to assign to it. Thus we can also use pointers as function arguments when "no object" may be a valid argument.

Pointers and arrays are closely related in C. In some respects, an array name behaves like a reference in C++. That is, the name refers to the start address of the array (the address of the first element of the array). As such, the name and the address of the name are one and the same, as is the address of the first element:

int a[100];

assert (a == &a);

assert (a == &a[0]);

The array subscript operator [] is nothing more than a sugar-coated abstraction to simplify the pointer arithmetic required to navigate an array. For any array a and any integer j within the range of a, we have the following equivalences:

a[j] j[a]

It often surprises people that a[j] and j[a] are equivalent, however the compiler will interpret j[a] as meaning *(j+a), which is nothing more than trivial pointer arithmetic. Such low-level cleverness does not belong in production code, of course.

User Avatar

Wiki User

6y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is the advantage of using pointer in c?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What is the major advantage of using C-V-P analysis?

What is the major advantage of using C-V-P graphs?


How do you change scrin coulor using far pointer in c ganguage?

brown


Call by reference using pointer in c plus plus?

Example: void foo( MyClass& object ){} // function with call by reference signature MyClass* p = new MyClass(); // instantiate a pointer to MyClass foo( *p ); // call by reference using the pointer


What is advantage of using file in c?

Storing data, mainly.


What are the extra uses of pointer such as print anything on the screen without using printf in c?

Your question makes no sense.


How do you print a far pointer value in a C programming environment using small memory model?

(*p)[23]


What is the uses of C V P?

What is the major advantage of using C-V-P graphs?


What is pointer to a member in objective c?

It is a pointer that points to a member of a structure.


How can you offset a pointer in C?

Increment or decrement the pointer by the required offset.


You want to write a simple without using pointer or array c program which will print greatest number when you give 20 number?

i want to write a simple without using pointer or array c program which will print greatest number when i give 20 number .........How far have you gotten so far?


Pointer arithemetic in C?

no


What is the purpose of pointer in C?

the purpose of pointer in c for saving the memory space.,and reduce the length and complexity of the program