answersLogoWhite

0


Best Answer

In order to copy a variable you must copy its value and store that value in another variable, thus creating an independent copy of the value. That is, after the copy, changing one value has no effect upon the other:

void f (int x) {

int x = y;

assert (x == y); // assert the values are the same

assert (&x != &y); // assert the variables occupy different addresses (independent variables)

}

To achieve the same thing with a pointer you must create an independent variable to store the copied value:

void g (int x) {

int* p = malloc (sizeof (int)); // allocate memory to hold the value

*p = x; // copy the value to the allocated memory

assert (*p == x); // assert the values are the same

assert (p != &x); // assert the variables occupy different addresses (independent variables)

// ...

free (p); // release the allocated memory

}

Note that the variable referred to by p is anonymous (has no name). To refer to the value we must dereference the pointer (*p).

Copying a value via a pointer is achieved similarly:

void h (int* p) { int x = *p; // copy the value referred to by p

assert (x == *p); // assert the values are the same

assert (&x != p); // assert the variables occupy different addresses (independent variables)

}

void i (int* p) {

int* p2 = malloc (sizeof (int)); // allocate memory to hold the value

*p2 = *p; // copy the value to the allocated memory

assert (*p2 == *p); // assert the values are the same

assert (p2 != p); // assert the variables occupy different addresses (independent variables)

// ...

free (p2); // release the allocated memory

}

User Avatar

Wiki User

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

Wiki User

6y ago

It doesn't. A pointer points to some data. If you copy the pointer, for example:pointerB = pointerA

then it will point to the SAME data, not to a copy. If you need a copy (so that you can modify the copy, without affecting the original), you need to create a separate variable, then copy the data.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How can a pointer create a perfect copy of a variable?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

How many pointers can be used in a c program?

Answergenerally we use simple pointer, void pointer,null pointer, structure pointer. Answerzero or more (unlimited).


How do you update a data file in c plus plus with a reference?

You cannot store references. A reference is nothing more than an alias, an alternate name for an existing variable or constant. References are primarily used when passing variables to functions such that the function can operate upon the variable itself -- known as passing by reference. The function refers to the variable by a different name, an alias, but it is the same variable. By contrast, when passing a variable by value the function uses a copy of that variable, assigning the variable's value to that copy. References are often confused with pointers, primarily because C uses the term to mean a pointer (hence the term, dereferencing). But in C++ a reference is a separate entity altogether. Unlike a reference, a pointer is a variable in its own right, one that can be used to store a memory address. Since a pointer has storage, you can store a pointer in a data file. However, in reality you are only storing the pointer's value -- a memory address -- not an actual pointer. Pointers and references are similar insofar as they can both refer to an object. A pointer does this by storing the memory address of the object, while a reference refers directly to the object itself. Thus if you have a pointer and a reference to the same object, the pointer's value is exactly the same as the address of the reference. Therefore the only way you can store a reference is by storing the object being referred to, not the reference itself.


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) }


How pointer differs from normal variable?

Usual variable used so called value type mechanism, meaning that if you have passed the variable to a function the variable itself was not passed, only its copy. Which makes value type mechanism safe. The only problem is that you use a lot of memory because a copy of your variable has been created.Pointers allow to avoid creating copies and operate with addresses of variables. It means when you pass your variable to any function, you actually pass only the variable's address. This mechanism is called reference type.Pointers work much faster and allow to use memory more effectively. The only problem is you have to take extra care when you are working with pointers. Using pointers any area of memory including protected by OS can be accessed. Such event will cause "blue screen" (under windows).


What is a bit copy?

A bit copy of an object is an exact, bit-by-bit, copy of that object. The default copy constructor generated by the compiler makes a bit copy. This is potentially a problem if the object contains pointers to other objects... A bit copy of a pointer copies the pointer, but not its data. This means that you have two pointers pointing at the same object in memory. If you delete one of them, the other becomes invalid, and this can (usually does) cause corruption. If an object contains a pointer, the object's copy constructor should provide for proper allocation and copying of any pointed to objects within that object.

Related questions

How many pointers can be used in a c program?

Answergenerally we use simple pointer, void pointer,null pointer, structure pointer. Answerzero or more (unlimited).


What is the difference between value type parameters and reference type parameters?

When a variable is passed by value, the function receives a copy of the variable. When a variable is passed by reference, the function receives a reference, or pointer, to the original data.


How do you update a data file in c plus plus with a reference?

You cannot store references. A reference is nothing more than an alias, an alternate name for an existing variable or constant. References are primarily used when passing variables to functions such that the function can operate upon the variable itself -- known as passing by reference. The function refers to the variable by a different name, an alias, but it is the same variable. By contrast, when passing a variable by value the function uses a copy of that variable, assigning the variable's value to that copy. References are often confused with pointers, primarily because C uses the term to mean a pointer (hence the term, dereferencing). But in C++ a reference is a separate entity altogether. Unlike a reference, a pointer is a variable in its own right, one that can be used to store a memory address. Since a pointer has storage, you can store a pointer in a data file. However, in reality you are only storing the pointer's value -- a memory address -- not an actual pointer. Pointers and references are similar insofar as they can both refer to an object. A pointer does this by storing the memory address of the object, while a reference refers directly to the object itself. Thus if you have a pointer and a reference to the same object, the pointer's value is exactly the same as the address of the reference. Therefore the only way you can store a reference is by storing the object being referred to, not the reference itself.


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) }


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.


How can you change values in call by reference?

We don't call by reference, we call functions. The arguments passed to the function are passed (not called) either by value or by reference, depending upon the function signature (the prototype). When you pass by reference you are passing the actual variable, not a copy of the variable, thus the function can modify that variable's value directly. The only exception is when the parameter is declared a constant reference. Passing a pointer is essentially the same as passing by reference, however the pointer itself is passed by value. To pass a pointer by reference you must pass a pointer-to-pointer instead. Passing by value always copies the value, whether it is declared constant or not. But if it is declared constant, the function might as well accept a constant reference. Passing objects (instances of a class) by constant value will incur a performance penalty in making an unnecessary copy. If it is constant, there is little point in copying the object.


How pointer differs from normal variable?

Usual variable used so called value type mechanism, meaning that if you have passed the variable to a function the variable itself was not passed, only its copy. Which makes value type mechanism safe. The only problem is that you use a lot of memory because a copy of your variable has been created.Pointers allow to avoid creating copies and operate with addresses of variables. It means when you pass your variable to any function, you actually pass only the variable's address. This mechanism is called reference type.Pointers work much faster and allow to use memory more effectively. The only problem is you have to take extra care when you are working with pointers. Using pointers any area of memory including protected by OS can be accessed. Such event will cause "blue screen" (under windows).


What is mint copy?

A perfect copy.


What is the difference between class variables and instance variables?

In the case of an instance variable, there is one copy for every instance (object). If you create 10 objects based on a class, there will be 10 copies of the variable. A class variable exists only once for the entire class - no matter how many objects you create - or even if you create no objects based on the class. In Java, such variables (class variables) are declared with the statickeyword.


What is the present perfect tense of copy?

The present perfect tense of copy is:I/You/We/They have copied.He/She/It has copied.


What is a bit copy?

A bit copy of an object is an exact, bit-by-bit, copy of that object. The default copy constructor generated by the compiler makes a bit copy. This is potentially a problem if the object contains pointers to other objects... A bit copy of a pointer copies the pointer, but not its data. This means that you have two pointers pointing at the same object in memory. If you delete one of them, the other becomes invalid, and this can (usually does) cause corruption. If an object contains a pointer, the object's copy constructor should provide for proper allocation and copying of any pointed to objects within that object.


What is pointer and pointer types and uses of pointer and the meaning of pointer?

The main advantages of using pointers are 1.) Function cannot return more than one value. But when the same function can modify many pointer variables and function as if it is returning more than one variable. 2.) In the case of arrays, we can decide the size of the array at runtime by allocating the necessary space. C has a minimum number of fundamental data types - a single character, a single integer or float, and a few derived data types such as a structure, an enumerated list, and an array. If you want to do much more than that, you make use of pointers. Pointers allow you to dynamically request memory to store off information for use later. They allow you to create linked lists and other algorithmically oriented data structures.