Podcast
Questions and Answers
In C, what is the correct way to define an integer pointer?
In C, what is the correct way to define an integer pointer?
- `*int ptr;`
- `int ptr;`
- `ptr int;`
- `int *ptr;` (correct)
In Java, you can directly print out the memory address of a reference.
In Java, you can directly print out the memory address of a reference.
False (B)
What operator is used in C to get the memory address of a variable?
What operator is used in C to get the memory address of a variable?
&
In C, the operator used to access the value stored at a memory address pointed to by a pointer is the ______ operator.
In C, the operator used to access the value stored at a memory address pointed to by a pointer is the ______ operator.
Match the pointer declarations with what they point to:
Match the pointer declarations with what they point to:
What is a key difference between references in Java and pointers in C?
What is a key difference between references in Java and pointers in C?
In C, it is possible to perform arithmetic operations on pointers.
In C, it is possible to perform arithmetic operations on pointers.
In the C programming language, what term describes converting a pointer back to the value it is pointing at?
In the C programming language, what term describes converting a pointer back to the value it is pointing at?
In terms of the 'no duplicates' code through, the main goal of the rmdup program is to remove ______.
In terms of the 'no duplicates' code through, the main goal of the rmdup program is to remove ______.
Match the following C pointer declarations with their descriptions:
Match the following C pointer declarations with their descriptions:
In C, if you have a pointer int *ptr
and you increment it using ptr++
, what happens?
In C, if you have a pointer int *ptr
and you increment it using ptr++
, what happens?
In C, arrays are always passed by value to functions.
In C, arrays are always passed by value to functions.
In C, what is the term for a pointer that does not point to any valid memory location?
In C, what is the term for a pointer that does not point to any valid memory location?
In C programs, function and variables definitions should not appears in ______ files.
In C programs, function and variables definitions should not appears in ______ files.
Match the following concepts related to memory addresses:
Match the following concepts related to memory addresses:
In C, what is the significance of the asterisk (*) in the declaration int *ptr;
?
In C, what is the significance of the asterisk (*) in the declaration int *ptr;
?
In Java, all variables of primitive types are reference variables.
In Java, all variables of primitive types are reference variables.
In the context of pointers, what does 'dereferencing' mean?
In the context of pointers, what does 'dereferencing' mean?
In C, strings are stored in ______ arrays that are null-terminated.
In C, strings are stored in ______ arrays that are null-terminated.
Match the following operations with their corresponding effects on a pointer:
Match the following operations with their corresponding effects on a pointer:
What is the purpose of the void *
pointer type in C?
What is the purpose of the void *
pointer type in C?
In C, arr = ptr;
is valid if arr
is an array and ptr
is a pointer of the same type.
In C, arr = ptr;
is valid if arr
is an array and ptr
is a pointer of the same type.
What is the primary use case for pointers?
What is the primary use case for pointers?
To access struct fields with an instance rather than a pointer utilize the ______ operator
To access struct fields with an instance rather than a pointer utilize the ______ operator
Match the C pointer arithmetic operations with their descriptions:
Match the C pointer arithmetic operations with their descriptions:
In the following C code snippet, what will be the output?
int x = 10;
int *y = &x;
int z = *y;
*y = 20;
printf("%d", x);
In the following C code snippet, what will be the output?
int x = 10;
int *y = &x;
int z = *y;
*y = 20;
printf("%d", x);
Pointers can point to only variables and not arrays.
Pointers can point to only variables and not arrays.
What is the default value of local and global variables?
What is the default value of local and global variables?
In memory, each piece of ______ has a corresponding address.
In memory, each piece of ______ has a corresponding address.
Match each code snippet with the output of the following C program
#include <stdio.h>
int main() {
int x1 = 10, x2 = 20;
int *p1, *p2;
p1 = &x1;
p2 = &x2;
*p1 = 100;
*p2 = *p1;
p1 = p2;
*p1 = 200;
printf("Value of x1 = %d\n",x1);
printf("Value of x2 = %d\n",x2);
return 0;
}
Match each code snippet with the output of the following C program
#include <stdio.h>
int main() {
int x1 = 10, x2 = 20;
int *p1, *p2;
p1 = &x1;
p2 = &x2;
*p1 = 100;
*p2 = *p1;
p1 = p2;
*p1 = 200;
printf("Value of x1 = %d\n",x1);
printf("Value of x2 = %d\n",x2);
return 0;
}
Given the following C code, what does the expression *ptr++
do?
int arr[] = {10, 20, 30};
int *ptr = arr;
// Expression: *ptr++
Given the following C code, what does the expression *ptr++
do?
int arr[] = {10, 20, 30};
int *ptr = arr;
// Expression: *ptr++
A pointer can only point to variables of the same data type as the pointer itself.
A pointer can only point to variables of the same data type as the pointer itself.
Explain the significance of initializing all variables, and especially pointers, in C programming.
Explain the significance of initializing all variables, and especially pointers, in C programming.
In Java, a variable of a ______ type stores the location of the object/array.
In Java, a variable of a ______ type stores the location of the object/array.
Associate operations and/or pointer arithmetic with their descriptions:
Associate operations and/or pointer arithmetic with their descriptions:
Which of the following statements about C arrays is correct?
Which of the following statements about C arrays is correct?
In C, the size of a pointer depends on the data type it points to.
In C, the size of a pointer depends on the data type it points to.
Differentiate between the address of operator (&) and the dereference operator (*).
Differentiate between the address of operator (&) and the dereference operator (*).
Given that a variable ptr
is a pointer, the expression ______ is used to access the value stored in the memory location pointed to by ptr
.
Given that a variable ptr
is a pointer, the expression ______ is used to access the value stored in the memory location pointed to by ptr
.
Match each term with corresponding description of a C Pointer:
Match each term with corresponding description of a C Pointer:
Given the C code snippet:
int x = 5;
int *p = &x;
*p = *p + 3;
What is the final value of x
?
Given the C code snippet:
int x = 5;
int *p = &x;
*p = *p + 3;
What is the final value of x
?
In C, strings are mutable, meaning their contents can be changed after they are created.
In C, strings are mutable, meaning their contents can be changed after they are created.
What should you do with all variables, especially pointers, when programming in C?
What should you do with all variables, especially pointers, when programming in C?
In C, a string must be terminated by a ______ value.
In C, a string must be terminated by a ______ value.
In Java, what does the concept of references mean?
In Java, what does the concept of references mean?
Flashcards
Memory
Memory
A contiguous array of bytes in memory, each with a unique address.
Primitive Type
Primitive Type
A type that directly holds a value (e.g., int, char).
Reference Type
Reference Type
A type that stores the location (address) of an object or array.
Java's Variable Types
Java's Variable Types
Signup and view all the flashcards
Pointer (in C)
Pointer (in C)
Signup and view all the flashcards
Reference (in C)
Reference (in C)
Signup and view all the flashcards
Pointer Manipulation
Pointer Manipulation
Signup and view all the flashcards
Instance Types in C
Instance Types in C
Signup and view all the flashcards
Pointer Types in C
Pointer Types in C
Signup and view all the flashcards
Arrow Operator (->)
Arrow Operator (->)
Signup and view all the flashcards
Struct (in C)
Struct (in C)
Signup and view all the flashcards
Address Of Operator (&)
Address Of Operator (&)
Signup and view all the flashcards
Dereference Operator (*)
Dereference Operator (*)
Signup and view all the flashcards
C Arrays
C Arrays
Signup and view all the flashcards
Array Name as a Reference / Pointer
Array Name as a Reference / Pointer
Signup and view all the flashcards
Strings in C
Strings in C
Signup and view all the flashcards
What is a pointer?
What is a pointer?
Signup and view all the flashcards
Address-of operator
Address-of operator
Signup and view all the flashcards
Defining a Pointer
Defining a Pointer
Signup and view all the flashcards
What is Dereference operator
What is Dereference operator
Signup and view all the flashcards
Using Pointers in Functions
Using Pointers in Functions
Signup and view all the flashcards
Pointer vs array
Pointer vs array
Signup and view all the flashcards
Pointer Arithmetic
Pointer Arithmetic
Signup and view all the flashcards
Study Notes
- Memory is an array of bytes, each piece of data in memory has an address.
- Java has no notion of memory.
References and Instances in Java
- There are two kinds of types in Java: Primitive and Reference
- Primitive types include long, int, short, char, boolean, double, and float
- Reference types refers to classes and arrays
- A variable of primitive type stores the actual value.
int x = 7;
is an example of storing the actual value.- A variable of a reference type stores the location of an object or array.
String work = "Hello";
stores the location of the object.
What is the Value?
- Static variables retain their value between function calls.
- In Java, a variable is not assigned a value, it will remain as the initial value and not be updated in the function.
- In C, if an object value is updated then this will be reflected in main.
No Choice in Java
- Java does not give a choice of whether a variable is a reference or instance.
- All variables of primitive type are instance variables.
- All variables of class or array type are reference variables.
References V/S Pointers
- C has pointers.
- A reference refers to an object or an array and is implementation dependent
- References are not manipulated by the program and will either refer to a valid object/array or be null.
- A pointer is a variable that stores a memory address, and is hardware dependent.
- Pointers can be manipulated by the program.
- Pointers can hold a memory address (valid or invalid) or be null and be a way to reference a memory location.
Java has References
- In Java, it is not possible to print out or manipulate the value of a reference.
- A valid object reference can be stored.
- It’s possible to assign
String word = null;
- You cannot store an arbitrary value in a reference.
C has Pointers
- In C you can print out and manipulate the value of a pointer variable
- A pointer is simply an integer, interpreted as a memory address
- A valid memory address can be stored:
char * word = “Hello”;
- An invalid memory address can be stored:
char * word = 42;
char * word = NULL;
is valid.- It’s possible to store an arbitrary value in a pointer.
- You can add, subtract, and multiply pointers.
Two Kinds of Types in C
- Instance types: Variable stores the value itself
- Pointer Types: Variable stores the address of the value
- A pointer can point to a variable of any type.
- Placing the
*
after the type indicates that it is pointer to variables of that type. - Use the
->
operator to access fields when using a pointer to a struct, instead of.
.
Aside on Structs
- Structs are like classes except there are no methods, all fields are public, all fields are instance variables.
- The variables of a struct are accessed using the
.
operator. struct node
is a type.
What Does a Pointer Point To?
int *val;
represents a pointer to an int.double *temp;
represents a pointer to a double.struct queue *q;
represents a pointer to a struct queue.char *word;
represents a pointer to a char or string.int **vp;
represents a pointer to an integer pointer.void *data;
represents a pointer to any kind of value.void ***ouch;
represents a pointer to a pointer to a pointer to any kind of value.
Using Pointers: Address Of (&) and Dereferencing (*)
- Two fundamental operations: Point a pointer to data and Obtain the data pointed to by a pointer.
- To point a pointer, get the address of the data and store the address in the pointer.
- To get the data at a pointer, dereference the pointer.
The Address of Operator (&)
- C provides an "address of" operator (&) to get it a variable's address.
- Place it the
&
operator before the variable whose address is desired to use it. - If a variable V is of type T, then &V is of type T*
- Every variable in a program is located in memory and hence has an address.
- The
&
operator is also a bitwiseAND
when used as a binary operator.
The Dereference Operator (*)
- C provides the dereference operator
*
to get the value being pointed to. - Place it the
*
operator before the variable whose address is desired to use it. - If a pointer P is of type T*, then *P is of type T
C Arrays
- Array variables are defined by specifying the array size after the name:
int values[100]; // define an array of 100 integers
- Arrays are accessed in the same way as in Java.
- Arrays can be defined either as local, static, or global variables.
- Arrays are used to store strings in C
C References
- Consider an array in C:
int values[100];
- The array name is a reference to the start of the array:
int * ptr = values;
values
refers to the 0th element - A reference is an address that cannot be modified.
values = ptr;
would generate a compiler error - There are other kinds of reference in C.
- References to arrays also allow to easily manipulate strings.
Strings in C
- Strings are stored in chracter arrays in C
- Strings in C are null-terminated, meaning that the last character should be a nul (\0).
- A string literal is the same as in Java, e.g., “Hello, World!\n"
- The value of the string literal is the address of the first character in memory char *word = “Hello, World!\n”;
- Arrays are used to store strings in C.
When Writing C Programs, Follow These Rules!
- Never put function or variable definitions in header files; it will cause the linker to complain about multiple definitions of the same symbol.
- Every code file, except the one containing main() should have a corresponding header file.
- All functions and variables that are used outside of a code file should be declared (not defined) in the corresponding header file.
- Initialize all variables, especially pointers!
- The default value of local and global variables is garbage.
Memory Locations Q&A
- Q: What happens when you define a variable, e.g. int x?
- A: It is assigned a space in memory.
- Q: What value does it hold?
- A: Any value present at that memory location (non-static).
- Q: What happens when you initialize it (e.g. x=10)?
- A: The value is written in the memory allocated to it
Address of a Variable Q&A
- Q: How do you get the memory address of that variable?
- A: Using the Address-of operator - &.
- Example: &x returns 527
- Q: Where can we store this address of a variable?
- A: In a pointer!
Pointers Q&A
- Q: What is a pointer?
- A: Variable that stores memory addresses, usually of other variables
- Q: Why do we need pointers?
- A: Dynamically allocate memory – you can write programs that can handle unlimited amounts of memory.
- Allow a function to modify a variable passed to it.
- Easier to pass around the location of a huge amount of data than passing the data itself.
Pointers Definition Q&A
- Q: How do you define a pointer?
- A:
<variable type> *ptr_name;
- Example:
int *y;
Note that the pointer's type is not int, but rather the variable that the pointer points to is int
- A:
- The pointer's definition needs to include the data type it is going to point to:
- Example:
int x;
int *y;
y = &x;
- Example:
- y is the pointer variable and contains the memory address of the integer variable x
Dereference Operator
Q: How can you get the content of the memory address pointed to by the pointer?
- A: By using the dereference operator
*
. - Read the
*
operator as "value pointed to by". - Example:
int x = 10;
int *y = &x; pointer variable
y = Address` of xint z = *y;
z = "value pointed to by y" = x, i.e. z = x;
Pointers versus Derefence Operators
int x = 10; //variable x = 10
int * y = &x; pointer //variable y = Address of x
int z = *y; value //pointed to by y” = x, i.e. z = x;
*y = 20; value pointed to by y" = 20;
Pointers and Arrays
- Arrays can be implicitly converted to the pointer of the proper type
- Example:
int arr[20];
int *ptr;
ptr = arr;
- Arrays can be used just like a pointer to its first element
- Pointer arithmetic, such as
ptr++
, will offset the memory location by the size of the underlying type. - Pointers and arrays support the same set of operations
- Pointers can be assigned new addresses, while arrays cannot
Pointer Arithmetic
- Only addition and subtraction operations are allowed on pointers
- What happens in the following cases?
- * p++ *(p++)//value pointed to by p, then increment pointer
- **++p* *(++p) //increment pointer, then value pointed to by p
- *++p
++(*p)
//increment value pointed to by p - *(p)++
(*p)++
//value pointed to by p, then increment value - *p++ = *q++
*p = *q; ++p; ++q
Pointer Arithmetic
- Only addition and subtraction operations are allowed on pointers
Pointer as Function Argument
- Pointers as function arguments allow for changing the values of different variables in memory.
- When a variable is passed to this function, it’s address is taken and its value can be changed.
- Functions that involve pointers take that as their argument instead.
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.
Related Documents
Description
Learn about primitive and reference types in Java, including how variables store values or object locations. Understand the behavior of static variables and how Java handles variable assignments compared to languages like C. Discover Java's automatic management of references and instances.