Program Design
Help Questions
AP Computer Science A › Program Design
USING POINTERS
Study the following pseudocode.
**int * var1;**
**int foo = 63;**
**var1 = &foo;**
**var2 = *var1;**
What are the values of var1 and var2?
var1 is assigned the memory address value of foo.
var2 is assigned a value of 63
var1 is assigned a value of 63
var2 is assigned a value off 63
var1 is assigned a value of 63
var2 is assigned the memory address value of var1
var1 is assigned the memory address value of foo
var2 is assigned the memory address value of var1
Explanation
Pointers store the address of another variable. Pointers are declared by naming the type, then an asterisk, followed by the name of the variable. In our example, var1 was declared a pointer that points to an integer:
int * var1;
Next in the code, we see that an integer variable named "foo" is created and is assigned a value of 63.
The address-of operator (&) is then used to get the address of a variable. Now lets take a look at the next line of the code:
var1 = &foo;
Here the ampersand is used to get the address of foo within memory. This means that var1 contains the address value of where foo is stored in memory.
Next in the code, we have the following statement:
**var2 = *var1;**
Here the dereference operator (*) is being used. This operator is used whenever we want to get the value (not the address) of the variable that a pointer is pointing to. In this case, var1 is storing the address of foo. However, by using the dereference operator we can get the actual value of the variable whose address is being stored in var1. In this case, dereferencing var1, we get the actual value of foo which is 63. Therefore, var2 = 63.
USING POINTERS
Study the following pseudocode.
**int * var1;**
**int foo = 63;**
**var1 = &foo;**
**var2 = *var1;**
What are the values of var1 and var2?
var1 is assigned the memory address value of foo.
var2 is assigned a value of 63
var1 is assigned a value of 63
var2 is assigned a value off 63
var1 is assigned a value of 63
var2 is assigned the memory address value of var1
var1 is assigned the memory address value of foo
var2 is assigned the memory address value of var1
Explanation
Pointers store the address of another variable. Pointers are declared by naming the type, then an asterisk, followed by the name of the variable. In our example, var1 was declared a pointer that points to an integer:
int * var1;
Next in the code, we see that an integer variable named "foo" is created and is assigned a value of 63.
The address-of operator (&) is then used to get the address of a variable. Now lets take a look at the next line of the code:
var1 = &foo;
Here the ampersand is used to get the address of foo within memory. This means that var1 contains the address value of where foo is stored in memory.
Next in the code, we have the following statement:
**var2 = *var1;**
Here the dereference operator (*) is being used. This operator is used whenever we want to get the value (not the address) of the variable that a pointer is pointing to. In this case, var1 is storing the address of foo. However, by using the dereference operator we can get the actual value of the variable whose address is being stored in var1. In this case, dereferencing var1, we get the actual value of foo which is 63. Therefore, var2 = 63.
USING POINTERS
Study the following pseudocode.
**int * var1;**
**int foo = 63;**
**var1 = &foo;**
**var2 = *var1;**
What are the values of var1 and var2?
var1 is assigned the memory address value of foo.
var2 is assigned a value of 63
var1 is assigned a value of 63
var2 is assigned a value off 63
var1 is assigned a value of 63
var2 is assigned the memory address value of var1
var1 is assigned the memory address value of foo
var2 is assigned the memory address value of var1
Explanation
Pointers store the address of another variable. Pointers are declared by naming the type, then an asterisk, followed by the name of the variable. In our example, var1 was declared a pointer that points to an integer:
int * var1;
Next in the code, we see that an integer variable named "foo" is created and is assigned a value of 63.
The address-of operator (&) is then used to get the address of a variable. Now lets take a look at the next line of the code:
var1 = &foo;
Here the ampersand is used to get the address of foo within memory. This means that var1 contains the address value of where foo is stored in memory.
Next in the code, we have the following statement:
**var2 = *var1;**
Here the dereference operator (*) is being used. This operator is used whenever we want to get the value (not the address) of the variable that a pointer is pointing to. In this case, var1 is storing the address of foo. However, by using the dereference operator we can get the actual value of the variable whose address is being stored in var1. In this case, dereferencing var1, we get the actual value of foo which is 63. Therefore, var2 = 63.
USING POINTERS
Study the following pseudocode.
**int * var1;**
**int foo = 63;**
**var1 = &foo;**
**var2 = *var1;**
What are the values of var1 and var2?
var1 is assigned the memory address value of foo.
var2 is assigned a value of 63
var1 is assigned a value of 63
var2 is assigned a value off 63
var1 is assigned a value of 63
var2 is assigned the memory address value of var1
var1 is assigned the memory address value of foo
var2 is assigned the memory address value of var1
Explanation
Pointers store the address of another variable. Pointers are declared by naming the type, then an asterisk, followed by the name of the variable. In our example, var1 was declared a pointer that points to an integer:
int * var1;
Next in the code, we see that an integer variable named "foo" is created and is assigned a value of 63.
The address-of operator (&) is then used to get the address of a variable. Now lets take a look at the next line of the code:
var1 = &foo;
Here the ampersand is used to get the address of foo within memory. This means that var1 contains the address value of where foo is stored in memory.
Next in the code, we have the following statement:
**var2 = *var1;**
Here the dereference operator (*) is being used. This operator is used whenever we want to get the value (not the address) of the variable that a pointer is pointing to. In this case, var1 is storing the address of foo. However, by using the dereference operator we can get the actual value of the variable whose address is being stored in var1. In this case, dereferencing var1, we get the actual value of foo which is 63. Therefore, var2 = 63.
USING POINTERS
Study the following pseudocode.
**int * var1;**
**int foo = 63;**
**var1 = &foo;**
**var2 = *var1;**
What are the values of var1 and var2?
var1 is assigned the memory address value of foo.
var2 is assigned a value of 63
var1 is assigned a value of 63
var2 is assigned a value off 63
var1 is assigned a value of 63
var2 is assigned the memory address value of var1
var1 is assigned the memory address value of foo
var2 is assigned the memory address value of var1
Explanation
Pointers store the address of another variable. Pointers are declared by naming the type, then an asterisk, followed by the name of the variable. In our example, var1 was declared a pointer that points to an integer:
int * var1;
Next in the code, we see that an integer variable named "foo" is created and is assigned a value of 63.
The address-of operator (&) is then used to get the address of a variable. Now lets take a look at the next line of the code:
var1 = &foo;
Here the ampersand is used to get the address of foo within memory. This means that var1 contains the address value of where foo is stored in memory.
Next in the code, we have the following statement:
**var2 = *var1;**
Here the dereference operator (*) is being used. This operator is used whenever we want to get the value (not the address) of the variable that a pointer is pointing to. In this case, var1 is storing the address of foo. However, by using the dereference operator we can get the actual value of the variable whose address is being stored in var1. In this case, dereferencing var1, we get the actual value of foo which is 63. Therefore, var2 = 63.
USING POINTERS
Study the following pseudocode.
**int * var1;**
**int foo = 63;**
**var1 = &foo;**
**var2 = *var1;**
What are the values of var1 and var2?
var1 is assigned the memory address value of foo.
var2 is assigned a value of 63
var1 is assigned a value of 63
var2 is assigned a value off 63
var1 is assigned a value of 63
var2 is assigned the memory address value of var1
var1 is assigned the memory address value of foo
var2 is assigned the memory address value of var1
Explanation
Pointers store the address of another variable. Pointers are declared by naming the type, then an asterisk, followed by the name of the variable. In our example, var1 was declared a pointer that points to an integer:
int * var1;
Next in the code, we see that an integer variable named "foo" is created and is assigned a value of 63.
The address-of operator (&) is then used to get the address of a variable. Now lets take a look at the next line of the code:
var1 = &foo;
Here the ampersand is used to get the address of foo within memory. This means that var1 contains the address value of where foo is stored in memory.
Next in the code, we have the following statement:
**var2 = *var1;**
Here the dereference operator (*) is being used. This operator is used whenever we want to get the value (not the address) of the variable that a pointer is pointing to. In this case, var1 is storing the address of foo. However, by using the dereference operator we can get the actual value of the variable whose address is being stored in var1. In this case, dereferencing var1, we get the actual value of foo which is 63. Therefore, var2 = 63.
USING POINTERS
Study the following pseudocode.
**int * var1;**
**int foo = 63;**
**var1 = &foo;**
**var2 = *var1;**
What are the values of var1 and var2?
var1 is assigned the memory address value of foo.
var2 is assigned a value of 63
var1 is assigned a value of 63
var2 is assigned a value off 63
var1 is assigned a value of 63
var2 is assigned the memory address value of var1
var1 is assigned the memory address value of foo
var2 is assigned the memory address value of var1
Explanation
Pointers store the address of another variable. Pointers are declared by naming the type, then an asterisk, followed by the name of the variable. In our example, var1 was declared a pointer that points to an integer:
int * var1;
Next in the code, we see that an integer variable named "foo" is created and is assigned a value of 63.
The address-of operator (&) is then used to get the address of a variable. Now lets take a look at the next line of the code:
var1 = &foo;
Here the ampersand is used to get the address of foo within memory. This means that var1 contains the address value of where foo is stored in memory.
Next in the code, we have the following statement:
**var2 = *var1;**
Here the dereference operator (*) is being used. This operator is used whenever we want to get the value (not the address) of the variable that a pointer is pointing to. In this case, var1 is storing the address of foo. However, by using the dereference operator we can get the actual value of the variable whose address is being stored in var1. In this case, dereferencing var1, we get the actual value of foo which is 63. Therefore, var2 = 63.
USING POINTERS
Study the following pseudocode.
**int * var1;**
**int foo = 63;**
**var1 = &foo;**
**var2 = *var1;**
What are the values of var1 and var2?
var1 is assigned the memory address value of foo.
var2 is assigned a value of 63
var1 is assigned a value of 63
var2 is assigned a value off 63
var1 is assigned a value of 63
var2 is assigned the memory address value of var1
var1 is assigned the memory address value of foo
var2 is assigned the memory address value of var1
Explanation
Pointers store the address of another variable. Pointers are declared by naming the type, then an asterisk, followed by the name of the variable. In our example, var1 was declared a pointer that points to an integer:
int * var1;
Next in the code, we see that an integer variable named "foo" is created and is assigned a value of 63.
The address-of operator (&) is then used to get the address of a variable. Now lets take a look at the next line of the code:
var1 = &foo;
Here the ampersand is used to get the address of foo within memory. This means that var1 contains the address value of where foo is stored in memory.
Next in the code, we have the following statement:
**var2 = *var1;**
Here the dereference operator (*) is being used. This operator is used whenever we want to get the value (not the address) of the variable that a pointer is pointing to. In this case, var1 is storing the address of foo. However, by using the dereference operator we can get the actual value of the variable whose address is being stored in var1. In this case, dereferencing var1, we get the actual value of foo which is 63. Therefore, var2 = 63.
USING POINTERS
Study the following pseudocode.
**int * var1;**
**int foo = 63;**
**var1 = &foo;**
**var2 = *var1;**
What are the values of var1 and var2?
var1 is assigned the memory address value of foo.
var2 is assigned a value of 63
var1 is assigned a value of 63
var2 is assigned a value off 63
var1 is assigned a value of 63
var2 is assigned the memory address value of var1
var1 is assigned the memory address value of foo
var2 is assigned the memory address value of var1
Explanation
Pointers store the address of another variable. Pointers are declared by naming the type, then an asterisk, followed by the name of the variable. In our example, var1 was declared a pointer that points to an integer:
int * var1;
Next in the code, we see that an integer variable named "foo" is created and is assigned a value of 63.
The address-of operator (&) is then used to get the address of a variable. Now lets take a look at the next line of the code:
var1 = &foo;
Here the ampersand is used to get the address of foo within memory. This means that var1 contains the address value of where foo is stored in memory.
Next in the code, we have the following statement:
**var2 = *var1;**
Here the dereference operator (*) is being used. This operator is used whenever we want to get the value (not the address) of the variable that a pointer is pointing to. In this case, var1 is storing the address of foo. However, by using the dereference operator we can get the actual value of the variable whose address is being stored in var1. In this case, dereferencing var1, we get the actual value of foo which is 63. Therefore, var2 = 63.
USING POINTERS
Study the following pseudocode.
**int * var1;**
**int foo = 63;**
**var1 = &foo;**
**var2 = *var1;**
What are the values of var1 and var2?
var1 is assigned the memory address value of foo.
var2 is assigned a value of 63
var1 is assigned a value of 63
var2 is assigned a value off 63
var1 is assigned a value of 63
var2 is assigned the memory address value of var1
var1 is assigned the memory address value of foo
var2 is assigned the memory address value of var1
Explanation
Pointers store the address of another variable. Pointers are declared by naming the type, then an asterisk, followed by the name of the variable. In our example, var1 was declared a pointer that points to an integer:
int * var1;
Next in the code, we see that an integer variable named "foo" is created and is assigned a value of 63.
The address-of operator (&) is then used to get the address of a variable. Now lets take a look at the next line of the code:
var1 = &foo;
Here the ampersand is used to get the address of foo within memory. This means that var1 contains the address value of where foo is stored in memory.
Next in the code, we have the following statement:
**var2 = *var1;**
Here the dereference operator (*) is being used. This operator is used whenever we want to get the value (not the address) of the variable that a pointer is pointing to. In this case, var1 is storing the address of foo. However, by using the dereference operator we can get the actual value of the variable whose address is being stored in var1. In this case, dereferencing var1, we get the actual value of foo which is 63. Therefore, var2 = 63.