answersLogoWhite

0

What is the use of pointer in c?

Updated: 8/10/2023
User Avatar

Wiki User

12y ago

Best Answer

A double pointer in C or C++ ...

int ** ppi;

... simply means that ppi is a pointer that points to a pointer that points to an int.

When defining function-parameters, another way of declaring this is ...

int * ppi[];

... which means that ppi is a pointer to an array of pointers that each point to an int, which happens to have the exact same meaning, but it is more telling in terms of what the usefulness of such a double pointer might have.

Think of main() ...

int main (int agrc, char ** argv);

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

... the two forms have exactly the same meaning, but the second form more clearly says what the design paradigm is, that argv is a pointer to an array of pointers that each point to an array of char, i.e. the arguments of the program's invocation.

User Avatar

Wiki User

12y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

15y ago

It has to do with the memory model you are using... If you are using the LARGE or HUGE memory model, then you use HUGE memory pointers. == Huge pointers are like far pointers, just more so: they are always normalized (ie offset part is between 0 and 15), and segment wrapping-around do not occur if you use them.

This answer is:
User Avatar

User Avatar

Wiki User

8y ago

A pointer is a primitive variable that can be used to store a memory address. We say the variable "points to" the stored address, hence we call them pointers. The primary operator of a pointer is the dereference operator (prefix *) which allows us to access the value stored at the address being pointed at. Being a variable, pointers also have an address of their own, thus we can use a pointer to point at another pointer.

Like all variables, pointers must be declared with a type although we can use void* if the type cannot be established at compile time. When dereferencing a void* pointer, we must cast the pointer to the appropriate type.

Unlike a named variable which always refers to the same memory address, a pointer can refer to any memory address and we can change that address at any time unless the pointer is declared a constant pointer (in which case it must be initialised at the point of declaration). We can also change the value being pointed at unless the pointer is declared a pointer to constant. Thus for any given type, we have four possible pointer declarations:

  1. pointer to variable.
  2. pointer to constant.
  3. constant pointer to variable.
  4. constant pointer to constant.

Before we can use a pointer we must first assign an address to it and thus initialise it. If we have no suitable address to initialise the pointer, we must assign the all-zeroes bit pattern. On most implementations, the NULL macro is defined to match the all-zeroes bit pattern of the system, which is typically 32 bits in length on a 32-bit system and 64 bits on a 64-bit system.

As soon as the memory being pointed at falls from scope, we must assign NULL to the pointer. This ensures that we don't accidently attempt to dereference memory that is no longer valid (may have been overwritten) or has been released back to the system (no longer belongs to our program). Dereferencing a NULL pointer results in undefined behaviour.

Pointers are primarily used to allow the call by reference semantic. Unlike C++, C has no built-in reference type thus all arguments are passed to functions by value. However, by passing a pointer (by value) we are effectively passing by reference because the value of a pointer is a memory address.

Pointers also have an important role to play with regards arrays, especially variable-length arrays allocated dynamically on the heap (the free store). Whenever we allocate an array, we are setting aside a chunk of memory for one or more elements of a given type. If the array is allocated in static memory or on the stack (local memory), we can name the array just as we can an ordinary variable because the length of the array is known at compile time (fixed-length arrays). Thus the array name refers to the start address of the allocation. But if the array is allocated on the heap, we must use a pointer variable to store the start address. Regardless, the elements of an array are anonymous (we cannot refer to elements by name) with the exception of named arrays where the first element (only) can be referred to by the array name.

Although they have no names of their own, every array element has identity (a memory address). Given that each element is of the same type and therefore the same length, it is trivial to calculate the address of each element by its zero-based index from the start of the array. Trivial as it is, the array suffix operator allows us to access elements without resorting to pointer arithmetic (the compiler generates the pointer arithmetic for us Behind the Scenes).

Arrays implicitly convert to pointers at the slightest provocation. This makes it impossible to pass arrays by value because once converted to a pointer, the length of the array is lost. Thus arrays are always passed by reference and the length of the array (its dimension) is passed via a separate argument.

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

A pointer is a variable that can store a reference (a memory address) or can store a NULL value (zero). The only use of any pointer in C++ is whenever you need to refer to memory that, for whatever reason, may not be valid. That is, if there is any possibility that a reference could be NULL, then you must use a pointer, because NULL references are invalid in both C and C++.



By way of an example, when you dynamically instantiate a new object at runtime, there is no way to guarantee that a sufficient block of free memory will be available, therefore you cannot reference that memory. For that reason, the new operator returns a pointer, not a reference. This is no different to the way the malloc() function returns a pointer, not a reference, in C.


As well as dynamic allocations, pointers can also be used to pass objects to functions. While programmers are encouraged to pass objects by reference rather than by value, it is sometimes desirable to pass a NULL reference. But since references can never be NULL, a NULL pointer must be passed instead.


Since pointers are variables they are more flexible than references insofar as once a memory address is assigned to a reference, that reference cannot refer to any other memory address while it remains in scope. But pointers can be assigned to point at any reference, including a NULL reference, at any time.



This answer is:
User Avatar

User Avatar

Wiki User

13y ago

We can use pointers in user defined functions, the reason behind is we can access the actual parameters indirectly by using pointers. And also we can reduce the length of a complicated program into very small code. It is easy to manipulate with pointers.

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

sizeof (void *)

often 2, 4 or 8 bytes
sizeof (void *) often 2, 4 or 8 bytes

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

Accessing data by address.

This answer is:
User Avatar

User Avatar

Wiki User

15y ago

Four bytes.

This answer is:
User Avatar

Add your answer:

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

What pointer type will you use to implement a heterogeneous linked list in c?

void pointer


Which pointer do you use to resize a chart?

is C or D


Which pointer do you use to resize a chart on excel?

C


WHAT IS POINTER TO POINTER IN C POINTER?

Pointer in C is Memory Reference. It stores memory address of any variable, constant, function or something you later use in your programming. Pointer basically used to ease the referencing of variables and others or in polymorphism and inheritance.


What are the applications of double pointer in c?

insert or delete values both side.so use double pointer


When would you use a pointer and a reference variable?

pointer: to access data by address reference: there is no reference in C language


What is the use of null pointer?

Pointer is a variable that stores address of a variable . A NULL Pointer a pointer that doesn't point to anything, it is a literal zero .Some people ,notably C++ programmers, prefer to use 0 rather than NULL.


What is a ponters in c-programming?

with the help of pointers we able to store the memory location of any variable. In c the pointer variable is use to store the memory location of any variable. The pointer variable is define as a simple variable but in pointer variable use a special "*" character at the left most side of name of pointer variable. If any variable name have * it means it is a pointer variable it hold the memory location of variable.


What is pointer and use of pointer?

A pointer looks a bit like a stick. I use it to point at something. Read your text book, read C tutorials on the web and if you still cannot answer the question consider floor sweeping or basket weaving as a vocation.


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.


Pointer arithemetic in C?

no