answersLogoWhite

0

When do you use pointers in C?

Updated: 8/16/2019
User Avatar

Wiki User

14y ago

Best Answer

There are many uses of pointer in C. Pointers are efficient and elegant to use.

All string variables are pointers, and calling a function using a pointer allows the function to change the value globally. These are what you can do with pointers in C. 1) Returning multiple values by passing address of variables. eg. foo(&a,&b,&c); 2) When you want to modify the value passed to function. eg. scanf() function.

int n;

scanf("%d",&n); /* pass address of variable n so that scanf can change its value*/

3) When you need to pass large data structure to function, it is more efficient to pass pointer to structure than passing the entire structure. If you don't want to modify the structure members, you can use 'const' keyword which the ANSI-complaint C compiler will either warn or give error if you modify. eg strlen(const char *str) in string library should not modify the content of str.

4) Implementing 'goto' data structures are easy with pointers. eg. linked-list,binary trees, hash table.

5) You can allocate dynamic memory using pointers.

6) Pointers to function are used for call back function and jump table. eg qsort() and bsearch() functions require the caller to provide pointer to function to do the comparison.

User Avatar

Wiki User

14y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: When do you use pointers in C?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What is the need to use pointers in C programming?

find the address


Pointers are include in csharp or not?

Yes, you can use pointers in the C#, but to some extent. Links are added with more details.


In C you use the concept of pointers whereas there are no pointers used in JAVA why?

Pointers in C are generally the thing that gives learners the most trouble. When C code is not written correctly with respect to pointer use, the resulting bugs can often be very difficult to find and correct. On the other hand, pointers are absolutely necessary in some cases.The designers of Java wanted to make programming easier and hence avoided adding pointers to the language. Java does have object references which accomplish much of what pointers accomplish albeit in a safer way.


What is the difference between c plus plus and java programming?

Java doesn't have pointers. C++ has pointers.


How do you write a reverse function in C plus plus without pointers?

You can either use references or you can simply return the result by value. Note that in C++, unlike C, references are not the same as pointers. C++ references are aliases, alternate names for existing objects, whereas pointers are just variables that may contain a memory address that can be dereferenced.


What is stream pointer in c?

C does not have stream pointers.


Why use pointer in c language with file handling?

Don't store pointers in files, it makes no sense.


Why pointer is callded jewel of c language?

a pointer is a derived data type in c. pointers are undoubtedly one of the most distinct and exciting features of c language.it has added power and flexibility to the language. *pointers are more efficient in handling arrays and tables. *pointer can be used to support dynamic memory management. *pointers reduce length and complexity of programs. *increase the execution speed and thus reduce the program execution time. by following character's real power of c lies in proper use of pointers. pointer is called the jewel of c-language.


What is the use of learning C?

C and C++ will help you understand the inner workings of a computer with operations such as dealing with memory and pointers. It will do anything that you tell it to do, which is both an advantage and a disadvantage.


Can you use data structure in java?

Yes. Its just (sort of) like C and C++. The difference is that Java does not have pointers, it has references, but, philosophically, its the same thing.


What is decleration part in C programming?

pointers.


What is the difference between pointers in c and c plus plus?

Nothing.