How to Create Pointers in C
Steps
- Decide the type of the pointer (that is, the type of data the pointer will be pointing to). The following tips might help:
- If you are declaring a dynamic array, use the array items' data type.
- If you are declaring the pointer to access the data of a variable, use the same data type as the variable.
- If you are declaring the pointer to traverse a list structure, use the list node data type (usually a user created struct).
- If you are declaring the pointer to traverse a tree, use the data type of the tree node, or a pointer to the tree node type as the type (pointer to a pointer of tree node type!).
- Declare the pointer using a syntax like this: data-type * pointer-identifier; where
- data-type is the type you decided in step 1
- pointer-identifier is the identifier or name of the pointer variable
- Assign the pointer to an initial memory location. This can be done using one of the following methods:
- Allocating memory and pointing to it by the pointer: int * i = malloc(sizeof(int)*n); where n is the number of memory blocks to assign.
- Assigning the address of a variable to the pointer: int * i = & x; where "x" is an integer and (&) means address-of.
- Assigning an array identifier to the pointer: int * i = array1; where array1 is an integer array(int[] array1;).
- Assigning a reference to the pointer: int * i = a; where "a" is an integer reference (int & a;).
- Assigning another pointer to the pointer: int * i = z; where "z" is another integer pointer (int * z;)
- Whenever you need to extract the data item currently pointed to by the pointer, use the value-at-address operator (*): int x = *i; where i is an integer pointer.
- Use the indexing operator on the pointer as if it was an array whenever you want to get a memory location next to the pointer without actually advancing the pointer. For example, if you have an integer pointer i, you can use i[2] which will retrieve the integer that is after the integer immediately after the integer pointed to by the reference (the integer that is 2 integers after the current location). The pointer i will still be pointing to the same memory location. Another alternative to this is getting the value at the pointer 2 steps after this pointer: *(i + 2)
- Use the increment(++), decrement(--), += and -= operators whenever you need to change the current location. i += 5; will advance the integer pointer i 5 integers forward.
- After you finish using the pointer, if you allocated memory to that pointer, make sure you free the allocated memory using the free() function. (free(i); where i is a pointer)
4.5 ★ | 2 Vote
You should read it
May be interested
- How to Do a Presentation in Classdoing a presentation in class can be intimidating, but it does not have to be. this wikihow will give you lots of pointers on how to do a presentation in class with minimal stress. write note cards on index cards. write main ideas on your...
- Arithmetic pointer in C / C ++as explained in the main chapter, the cursor in c / c ++ is an address, which is a numeric value. therefore, you can perform arithmetic operations on a pointer as you would with numeric values. there are 4 arithmetic operators that can be used on pointers: ++, -, +, and -.
- How to use Firework to create web app without advertisingfirework is software to create app for website with full features like download, copy link, support mobile version, block ads, ...
- Array (Array) in C / C ++c / c ++ programming language provides data structures called arrays, stored in a set of data of the same type with fixed length. an array is used to store data sets, but it is useful if you think of an array of variables with the same type.
- How to create SSH key on Windowsprotecting your connections and data integrity is where secure shell (ssh) keys excel. ssh keygen is commonly used to gain secure access to remote servers and cloud services.
- How to create a sitemap for websitessome cms create your own sitemap for you. they are automatically updated when you add or remove pages and posts from the site. if your cms does not do this, then there is usually a plugin available to solve the problem.
- Cursor in Cpointer in the c language is easy to learn. some tasks in c language are made easier by pointers, and other tasks become more flexible, such as in memory allocation, which cannot be performed without using a cursor.
- Reference in C ++a reference variable is an alias, which is another name for an existing variable. when a reference is initialized with a variable, then either the variable name or the reference name can be used to reference that variable.
- Use CMD to create an 'undeleted' folder on Windowshave you ever thought that you could create an inviolable folder, no one can delete that folder or not? don't think not because of this you can completely do it. just use cmd without the support of any 3rd party software or applications.
- Read / write File in C ++so far, we have used the iostream standard library, provided cin and cout methods to read from standard input and write to the corresponding standard output.