Podcast
Questions and Answers
In C, what is a pointer?
In C, what is a pointer?
- A keyword used to define constant values.
- A data type that can only store integer values.
- A function that can only modify global variables.
- A variable that stores a memory address. (correct)
Java gives the programmer a choice of whether a variable is a reference or instance.
Java gives the programmer a choice of whether a variable is a reference or instance.
False (B)
What operator is used in C to get the address of a variable?
What operator is used in C to get the address of a variable?
&
In C, strings are stored in ______ arrays.
In C, strings are stored in ______ arrays.
Match the C pointer declaration with what it points to:
Match the C pointer declaration with what it points to:
What does the dereference operator (*) do in C?
What does the dereference operator (*) do in C?
A reference in C is hardware dependent.
A reference in C is hardware dependent.
What is the primary difference between a struct and a class in C?
What is the primary difference between a struct and a class in C?
In C, to access the members of a struct through a pointer, the -> operator is used instead of the ______ operator .
In C, to access the members of a struct through a pointer, the -> operator is used instead of the ______ operator .
Match the following C code snippets with their descriptions:
Match the following C code snippets with their descriptions:
Which of the following is a valid reason for using pointers in C?
Which of the following is a valid reason for using pointers in C?
In C, arrays can be assigned new addresses.
In C, arrays can be assigned new addresses.
What is the purpose of null-terminating strings in C?
What is the purpose of null-terminating strings in C?
In C, the address of the first element in an array is known as a ______ to the start of the array.
In C, the address of the first element in an array is known as a ______ to the start of the array.
Match the following pointer arithmetic operations with their descriptions:
Match the following pointer arithmetic operations with their descriptions:
Given int arr[5]; int *ptr = arr;
, what does ptr + 2
represent?
Given int arr[5]; int *ptr = arr;
, what does ptr + 2
represent?
In C, it is acceptable to perform multiplication operations on pointers.
In C, it is acceptable to perform multiplication operations on pointers.
What is the term for accessing the value stored at the memory location that a pointer points to?
What is the term for accessing the value stored at the memory location that a pointer points to?
In Java, a variable of ______ type stores the location of an object or array.
In Java, a variable of ______ type stores the location of an object or array.
Match the following C code elements with their purposes regarding pointers:
Match the following C code elements with their purposes regarding pointers:
Which rule should be followed when writing C?
Which rule should be followed when writing C?
In C, references will either refer to an invalid object/array or be null.
In C, references will either refer to an invalid object/array or be null.
What is the output of this code int x = 10; int * y = &x; int z = *y;
What is the output of this code int x = 10; int * y = &x; int z = *y;
In C, a pointer is considered a(n) ______ and is interpreted as memory address.
In C, a pointer is considered a(n) ______ and is interpreted as memory address.
Match the pointer with what it points to.
Match the pointer with what it points to.
What is the output of this code? int x1 = 10, x2 = 20; int * p1; p1 = &x1; *p1 = 100;
What is the output of this code? int x1 = 10, x2 = 20; int * p1; p1 = &x1; *p1 = 100;
A variable of a primitive type stores the address of an object or array.
A variable of a primitive type stores the address of an object or array.
A variable of what type should be initiated, especially in C?
A variable of what type should be initiated, especially in C?
In C, a pointer is a variable that stores a(n) ______.
In C, a pointer is a variable that stores a(n) ______.
Which of the following is the correct way to retrieve a memory address and apply a dereference operator
Which of the following is the correct way to retrieve a memory address and apply a dereference operator
What is the output of this code? int x1 = 10 , x2 = 20; int * p1, * p2; p1 = &x1; p2 = &x2; * p2 = * p1
What is the output of this code? int x1 = 10 , x2 = 20; int * p1, * p2; p1 = &x1; p2 = &x2; * p2 = * p1
Function definitions should be declared and defined in header files.
Function definitions should be declared and defined in header files.
What is the default value of local variables?
What is the default value of local variables?
The address of the variable can be stored in a(n) ______.
The address of the variable can be stored in a(n) ______.
Match variable with variable type in java:
Match variable with variable type in java:
What is the value of a in this code? static void foo(int a) { a = 42; } static void main(String [] args) { int a = 0; foo(a); System.out.println(a); }
What is the value of a in this code? static void foo(int a) { a = 42; } static void main(String [] args) { int a = 0; foo(a); System.out.println(a); }
A reference is manipulated by the program
A reference is manipulated by the program
How does Java handle memory compared to C?
How does Java handle memory compared to C?
To get the value you need to ______ the pointer.
To get the value you need to ______ the pointer.
How to access a variable of a struct.
How to access a variable of a struct.
Which of the following data types can a void *
pointer point to?
Which of the following data types can a void *
pointer point to?
In C, when you increment a pointer (e.g., ptr++
), the address it points to always increases by one byte, regardless of the data type it points to.
In C, when you increment a pointer (e.g., ptr++
), the address it points to always increases by one byte, regardless of the data type it points to.
Flashcards
Memory structure
Memory structure
Memory is structured as an array of bytes, each piece of data has an address.
Java types
Java types
Primitive types store the actual value, while reference types store the memory location of an object or array.
Pointers
Pointers
C language feature that stores the memory address of a variable and can be manipulated.
Reference
Reference
Signup and view all the flashcards
Pointer
Pointer
Signup and view all the flashcards
Java reference
Java reference
Signup and view all the flashcards
C Pointer
C Pointer
Signup and view all the flashcards
Instance type in C
Instance type in C
Signup and view all the flashcards
Pointer types in C
Pointer types 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
Reference
Reference
Signup and view all the flashcards
C Array
C Array
Signup and view all the flashcards
Strings in C
Strings in C
Signup and view all the flashcards
The '&' Symbol
The '&' Symbol
Signup and view all the flashcards
Code organization in c
Code organization in c
Signup and view all the flashcards
Pointer
Pointer
Signup and view all the flashcards
Dereference Operator
Dereference Operator
Signup and view all the flashcards
Pointers and arrays
Pointers and arrays
Signup and view all the flashcards
Why Use Pointers?
Why Use Pointers?
Signup and view all the flashcards
Pointer Definition
Pointer Definition
Signup and view all the flashcards
Value Pointed To
Value Pointed To
Signup and view all the flashcards
Printf statement
Printf statement
Signup and view all the flashcards
What Is converted?
What Is converted?
Signup and view all the flashcards
What is arithmetic?
What is arithmetic?
Signup and view all the flashcards
Passes by arguments?
Passes by arguments?
Signup and view all the flashcards
Study Notes
Memory
- Each piece of data stored in memory has an address.
- Java does not have a direct notion of memory manipulation, unlike C which uses pointers.
- Memory can be visualized as an array of bytes, where each byte has a unique address.
- In the example, the string "Hello, World!\n\0" is stored in memory, with each character occupying a byte.
References and Instances in Java
- Java has two main types of variables: primitive and reference.
- Primitive types store the actual value, and reference types store the location of an object or array.
- Primitive types include; long, int, short, char, boolean, double, and float.
- Reference types include; classes and arrays
Java Value Assignment
- In Java, if you pass a primitive variable to a function and modify it within the function, the original variable outside the function remains unchanged.
- If an integer is initialized to 0 and passed to function foo(), which changes it to 42, still maintains the value of 0 outside of the said function.
- When an array is passed to a function, bar(), which changes the first element to 42; this will affect the original array.
Choices in Java
- Java gives no choice of whether a variable is a reference or an instance; this is determined by its type.
- Variables of primitive type are always instance variables.
- Variables of class or array type are always reference variables.
References vs Pointers
- C uses pointers, while a reference refers to an object or an array.
- A reference is implementation-dependent and is not directly manipulated by the program.
- Pointers are variables that store a memory address.
- Pointers are hardware-dependent and are manipulated by the program.
- Pointers can hold a memory address, even if it's invalid or null.
- Pointers can also be used as references.
Java References
- A valid object reference or null can be stored.
- It's impossible to print or change the value of a reference in Java.
- You cannot store an specific value in a reference.
C pointers
- Pointers in C can be printed out and the value of the pointer variable can be manipulated.
- A pointer is simply an integer that is looked at as a memory address.
- A valid memory address, an invalid memory address, or NULL can be stored.
- With C pointers, arbitrary values, and addition, subtraction, and multiplication becomes possible.
Types in C
- Instance and pointer types are the two kinds of types in C.
- Instance types allow the variable to store the value itself.
- Pointer types are when the variable stores the address of the value.
- A pointer can point to a variable of any type.
- Placing the * after the type indicates it is a pointer to variables of that type.
- When accessing struct fields via pointer, the -> operator should be used; instead of the . operator.
Structs
- Structs are like classes, except there are no methods, and all fields are public and instance variables.
- Struct variables are accessed using the . operator.
struct_node is a type
Pointer values
int * val;
points to an int.double *temp;
points to a double.struct queue *q;
points to a struct queue.char * word;
points to a char or string.int ** vp;
points to an integer poster.void * data;
points to any kind of value.void ***ouch;
points to a pointer to a pointer to any kind of value.
Pointers
- To point a pointer, the address of the data must be determined, and stored in the pointer.
- To get the data at a pointer, it must be dereferrenced..
Address of operator
- C provides the "address of" operator (&) to get the address of a variable.
- If V is of type T, then &V is of type T*.
- Every variable in a program is located in memory, hence it has an address.
- The & operator acts as a bitwise AND when used as a binary operator.
Dereference Operator
- Dereferencing a value obtains its value; C uses a dereference operator (*).
- Use the dereference operator before the pointer to obtain the value being pointed to.
int val = 42; // integer val contains 42
int *ptr = &val; // pointer points to val
printf(“%d”, *ptr); // print the value pointed to by ptr
- If a pointer P is of type T*, then *P is of type T.
Arrays
- Array variables are defined by specifying the array size after the name.
int values[100]; // define an array of 100 integers
int n = 50;
char string[n]; // define an array of n chars
- Arrays can be defined as local, static, or global variables.
- Strings are stored using arrays in C.
C References
- In C, an array name is a constant reference to the array's starting address.
-
int values[100];
-int * ptr = values; // values refers to 0th element
- References are addresses that cannot be modified
- Assigning ptr to values
values = ptr;
would generate a compiler error
Strings in C
- Strings in C are stored in character (char) arrays, which means they can be viewed as an array of chars
- Strings are null-terminated, which means that the last character should be a nul (
\0
). - The value of the string literal is the address of the first character in memory.
C Rules
- It's bad practice to include function or variable "definitions" in
header
files. - Doing so will cause multiple code files which results in "multiple definitions error"
- Code files 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.
- Not initalizing local and global variables will return Garbage!
Program Statement
- The program
rmdup
reads in zero or more lines and outputs the lines, removing all duplicates. - Input all input is from the console (stdin).
- Zero or more lines, where each line is terminated by a new-line/ And each contain less than 255 characters and a maximum of 100 lines.
- Output produces the console all the repeated lines removed.
Types of memory locations.
- When you define a variable, memory is assigned and when you initialize it, the value is written in.
Use of pointers
- Pointers are variables that store memory addresses, often of other variables.
- Pointers are needed to dynamically allocate memory, modify variables, and pass large amounts of data efficiently.
int x;
int * y;
y = &x;
y
is the pointer variable and contains the memory address of the integer variablex
.- The pointer’s type is not int, but rather the variable that the pointer points to is an int.
Dereference Operators
- Dereference operators get the content of the memory address pointed to by the pointer, by using
'*'
. - The
'*'
operator can be read "value pointed to by"
int x = 10; //variable x = 10
int * y = &x; //pointer variable y = Address of x
int z = *y; //z = “value pointed to by y” = x, i.e. z = x;
Pointer to Array conversion
- Arrays can be implicitly converted to the pointer of the proper
-
int arr20;
-int * ptr;
-ptr = arr;
-arr=ptr would be invalid
- Arrays can be used like a pointer to its first element and can be used by same sets of operations.
- Pointers can be assigned for new addresses, while arrays cannot.
Pointer Arithmetic
- Addition and subtraction are the functions that can be performed for pointers.
*p++
takes the value pointed to by p, then increments the pointer;*(++p) //increment pointer, then value pointed to by p
increments the pointer, the value pointed to by p++(*p)
increments the value pointed to by p(*p)++
takes the value pointed to by p, then increments value*p++
take the value pointed to by p, then increments value
Function Arguements
- A pointer can be used as a function argument in C.
- Any dereferrencing on the pointer will affect the original value.
void chg_ptr(int* ptr)
{
- ptr = 200; //change value pointed to by ptr
}
int main()
{
int n = 1;
int *p = &n;
std::cout << *p << "\n"; //outputs 1
chg_ptr(p);
//change value pointed to by p
std::cout << *p << "\n";
//outputs 200
return 0;
}
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.
Related Documents
Description
Explanation of memory management in Java, covering memory addresses, primitive vs. reference types, and value assignments. Focuses on how Java handles memory differently from languages like C, and behavior of variables inside and outside functions.