answersLogoWhite

0


Best Answer

I presume you referring to the C pointer syntax where an asterisk operator may be suffixed to a type or prefixed to a variable. The following may help to clarify:

int*p;

int* q;

int *r;

int * s;

All four of these declarations are exactly the same (they are all pointer-to-int variables). Note that the physical position of the asterisk operator makes no difference whatsoever, no matter how much whitespace you use (whitespace is essentially ignored both before and after an operator).

Because the type is pointer-to-int, it usually makes sense to use the second variant to separate the variable's type from its name. However, this doesn't really work when declaring multiple pointer variables of the same type on the same line using the comma operator. Consider the following:

int* p, q, r, s;

While it is natural to assume p, q, r and s are all pointer-to-int types, they are not. The first, p, is the only pointer-to-int type, while all the others are just plain int types. If we really want 4 pointer-to-int types, we need to use the following declaration instead:

int *p, *q, *r, *s;

User Avatar

Wiki User

8y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is the difference between pointer before a variable and pointer after a variable?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

How are pointer different from other variables?

A pointer variable is a variable that contains the memory location of another variable or an array (or anything else in memory). Effectively, it points to another memory location. For standard variables, you define a type, assign a value to that variable or read the value from it, and you can also read the memory location (&n = memory location of n) of the variable. For pointers, you can point them to any variable, even another pointer, and you can get the value of the variable it points to (*p), the location of that variable in memory (p), or the address in memory of the pointer itself (&p). Consider: long n = 65; long *p; p = &n; Results: Type | Name | Location | Value long n 0xA3FF 65 long * p 0x31DF 0xA3FF So p points to n. Now, n = 65 &n = 0xA3FF p = 0xA3FF *p = 65 &p = 0x31DF You may find yourself having to use typecasts frequently when using pointers. Pointers are useful when passing data to functions. For instance, consider the following function: void add(int a, int b, int c) { c = a + b; } The problem here is that the computer copies each variable into a new memory location before passing them to the function as local variables. This function effectively does nothing. However, if you change the function to: void add(int a, int b, int *c) { c = a + b; } and call the function by passing in the location of the variable to the function: add(a,b,&c); then you can modify the variable itself. Pointers are also good for working with arrays: char *c = "Hello World"; int i=0; while (c[i] != 0x00) { cout << c[i]; c++ } //print one letter at a time.


Meaning c plus plus pointer to an array and pointer to a structure?

They both mean the same thing; an array is a type of data structure (a linear structure). A pointer variable is just a variable like any other, but one that is used to specifically store a memory address. That memory address may contain a primitive data type, an array or other data structure, an object or a function. The type of the pointer determines how the data being pointed at is to be treated. Pointers must always be initialised before they are accessed, and those that are not specifically pointing at any reference should always be zeroed or nullified with the NULL value. This ensures that any non-NULL pointer is pointing at something valid. Remember that pointer variables are no different to any other variable insofar as they occupy memory of their own, and can therefore point to other pointer variables.


How can I initialize a type that is a pointer to a struct in Go?

For the purpose of clarification, you cannot initialise types, you can only initialise variables (or constants) of a type. A variable (or constant) is known as an "instance" or "object" of the type. All instances of a type must be named. A pointer variable allows us to "refer" to a named object of the pointer's type. this is achieved by storing the memory address (the starting address) of that object. By changing the address stored in the pointer variable, the same pointer variable can be used to refer to different objects in memory. This is useful when passing objects to functions because if we pass objects directly (by name), the function receives a copy of the object. This is known as "pass by value". However, when a function expects a pointer variable rather than a named variable, the address of the object is copied instead. This is known as "pass by reference" and allows the function to operate (indirectly) upon the object being referenced rather than a copy of the object (any changes to a copy are not reflected in the original object). Passing by reference is particularly useful when the object is large and complex. To improve efficiency, we must avoid making any unnecessary copies of large or complex objects. Functions that do not modify an object are a prime example; there is no point in copying an object that will not be modified. If we wish a function to modify the object, we must pass the object by reference. The only time we should copy an object is when we're not interested in the modifications made by a function, or when we want to compare the changes that were made. In these cases we simply make a copy of the object before passing that copy to the function by reference. When working with concurrency, however, it is best to use pass by value semantics. In this way, each thread works with a local copy of your object, and thus avoids "race conditions", where one task is accessing an object that is being modified by another task, which can lead to unpredictable results. Pointer variables are said to "point at" the memory the refer to (hence they are called pointers). To access the value being pointed at, the pointer variable must be dereferenced. However, in the case of pointer to struct variables, the dereferencing is transparent. All variables in Go are implicitly initialised with the default "zero" value for its type. The zero value of a pointer variable is always "nil" (regardless of which type of pointer). You must never dereference a pointer variable that holds the nil value so always check the pointer is non-nil. Equally, you must never dereference a pointer to an object that no longer exists in memory. Always nil your pointers as soon as you are finished with them. The following example demonstrates how to initialise a pointer variable to a struct: package main import "fmt" type Point struct { X int Y int } func main () { v := Point{1, 2} // instantiate an object of type Point p := &v // assign the address of v to pointer p (p's type is inferred from v) fmt.Println(*p) // print the indirect value of v (dereference p) }


What is reference variable in java?

A reference is a memory address. In some languages, like C, memory addresses can be stored in pointer variables. Thus a pointer is a reference variable. They are said to point to the memory address, and allow indirect access to that memory address. Since they are variable, they can be re-assigned (unless declared constant, in which case they are not variable). However, in other languages, like C++, a reference is neither a pointer nor a variable -- it is an alias, an alternate name for an already existing object. References have no storage of their own thus they cannot be reassigned once initialised. In that respect they are like constant pointers. However, since they have storage of their own, there is no need to use indeirection. It's a bit like referrring to someone named William as Bill, Billy or Will -- they're all alternate names (aliases) for the same person.


What is the pointer adjust in multimeter?

On a traditional analog multimeter, there is a screw on the face of the meter to adjust the tension on the pointer. It lets you set the pointer to zero when there is no current, just like zeroing the bathroom scales before you step on them.

Related questions

Difference between void pointer and null pointer?

A Null pointer has the value 0. void pointer is a generic pointer introduced by ANSI. Before ANSI, char pointers are used as generic pointer. Generic pointer can hold the address of any data type. Pointers point to a memory address, and data can be stored at that address.


What is the difference between a do while loop and a for loop in c?

the counter variable cannot be initialized in while loop before entering into the block.


How are pointer different from other variables?

A pointer variable is a variable that contains the memory location of another variable or an array (or anything else in memory). Effectively, it points to another memory location. For standard variables, you define a type, assign a value to that variable or read the value from it, and you can also read the memory location (&n = memory location of n) of the variable. For pointers, you can point them to any variable, even another pointer, and you can get the value of the variable it points to (*p), the location of that variable in memory (p), or the address in memory of the pointer itself (&p). Consider: long n = 65; long *p; p = &n; Results: Type | Name | Location | Value long n 0xA3FF 65 long * p 0x31DF 0xA3FF So p points to n. Now, n = 65 &n = 0xA3FF p = 0xA3FF *p = 65 &p = 0x31DF You may find yourself having to use typecasts frequently when using pointers. Pointers are useful when passing data to functions. For instance, consider the following function: void add(int a, int b, int c) { c = a + b; } The problem here is that the computer copies each variable into a new memory location before passing them to the function as local variables. This function effectively does nothing. However, if you change the function to: void add(int a, int b, int *c) { c = a + b; } and call the function by passing in the location of the variable to the function: add(a,b,&c); then you can modify the variable itself. Pointers are also good for working with arrays: char *c = "Hello World"; int i=0; while (c[i] != 0x00) { cout << c[i]; c++ } //print one letter at a time.


What is a NULL Pointer Whether it is same as an uninitialized pointerin C language?

A null pointer is a pointer that holds the value zero (0), which simply means it does not point at anything in particular. It is an error to try and dereference memory address 0x0, as this address is reserved by the system, and will result in undefined behaviour. Pointers must be tested to ensure they hold a non-null address before being dereferenced. An uninitialised pointer is the same as any other uninitialised variable; no value has yet been assigned to it. The value of an uninitialised variable will be whatever value happens to reside in the memory allocated to that variable. All variables, including pointer variables, must be initialised before being accessed for the first time and most compilers will warn against accessing an uninitialised value. Dereferencing an uninitialised pointer has undefined behaviour.


What are the characteristic differences between Rhombus?

You need two things before you can discuss difference between them!You need two things before you can discuss difference between them!You need two things before you can discuss difference between them!You need two things before you can discuss difference between them!


What is the difference between the statement a plus plus and plus plus a?

This is used in languages such as C, C++ and Java. The difference is when the statement is executed. If placed before the variable, the increment is done before other operations, otherwise, after them. This is best shown in an example.a + b++ means to add first, then to increment the variable b.a + ++b means to increment b first, then to do the addition.Similarly, in a = b++, b is copied to a, and then increment; while in a = ++b, variable b is incremented before being copied.


Definition of pointer in C?

In C programming, a pointer is a variable that stores the memory address of another variable. Pointers allow for indirect access to the value stored at the memory location pointed to by the pointer. They are commonly used for dynamic memory allocation, as well as for passing arguments to functions by reference. Pointers are declared using the '' operator, and the value stored in a pointer can be accessed using the '' operator as well.


Was noel pointer sick before his death?

noel pointer still alive


What is the difference between now and before?

----


Meaning c plus plus pointer to an array and pointer to a structure?

They both mean the same thing; an array is a type of data structure (a linear structure). A pointer variable is just a variable like any other, but one that is used to specifically store a memory address. That memory address may contain a primitive data type, an array or other data structure, an object or a function. The type of the pointer determines how the data being pointed at is to be treated. Pointers must always be initialised before they are accessed, and those that are not specifically pointing at any reference should always be zeroed or nullified with the NULL value. This ensures that any non-NULL pointer is pointing at something valid. Remember that pointer variables are no different to any other variable insofar as they occupy memory of their own, and can therefore point to other pointer variables.


What is the difference between oreos before and now?

----


What is the number before a variable?

the coefficient of the variable