Lecture 4 Pointers
42 Questions
1 Views

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

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.

False (B)

What operator is used in C to get the address of a variable?

&

In C, strings are stored in ______ arrays.

<p>character</p> Signup and view all the answers

Match the C pointer declaration with what it points to:

<p>int *val; = Pointer to an integer double *temp; = Pointer to a double char *word; = Pointer to a character or string void *data; = Pointer to any kind of value</p> Signup and view all the answers

What does the dereference operator (*) do in C?

<p>It accesses the value stored at the memory address held by a pointer. (B)</p> Signup and view all the answers

A reference in C is hardware dependent.

<p>False (B)</p> Signup and view all the answers

What is the primary difference between a struct and a class in C?

<p>Structs have public members by default; classes have private members by default.</p> Signup and view all the answers

In C, to access the members of a struct through a pointer, the -> operator is used instead of the ______ operator .

<p>.</p> Signup and view all the answers

Match the following C code snippets with their descriptions:

<p>int *ptr = &amp;var; = Declares a pointer <code>ptr</code> and initializes it with the address of <code>var</code>. *ptr = 10; = Assigns the value 10 to the variable pointed to by <code>ptr</code>. int arr[5]; int *ptr = arr; = Declares an integer array <code>arr</code> and a pointer <code>ptr</code> initialized with the address of the first element of <code>arr</code>. ptr++; = Increments the pointer <code>ptr</code> to point to the next memory location.</p> Signup and view all the answers

Which of the following is a valid reason for using pointers in C?

<p>To directly manipulate memory addresses and implement data structures. (D)</p> Signup and view all the answers

In C, arrays can be assigned new addresses.

<p>False (B)</p> Signup and view all the answers

What is the purpose of null-terminating strings in C?

<p>To indicate the end of the string.</p> Signup and view all the answers

In C, the address of the first element in an array is known as a ______ to the start of the array.

<p>reference</p> Signup and view all the answers

Match the following pointer arithmetic operations with their descriptions:

<p>*p++; = Accesses the value pointed to by <code>p</code> and then increments <code>p</code>. *++p; = Increments <code>p</code> and then accesses the value pointed to by <code>p</code>. ++*p; = Increments the value pointed to by <code>p</code>. (*p)++; = Increments the value pointed to by <code>p</code>.</p> Signup and view all the answers

Given int arr[5]; int *ptr = arr;, what does ptr + 2 represent?

<p>The address of the third element in <code>arr</code>. (D)</p> Signup and view all the answers

In C, it is acceptable to perform multiplication operations on pointers.

<p>True (A)</p> Signup and view all the answers

What is the term for accessing the value stored at the memory location that a pointer points to?

<p>Dereferencing</p> Signup and view all the answers

In Java, a variable of ______ type stores the location of an object or array.

<p>reference</p> Signup and view all the answers

Match the following C code elements with their purposes regarding pointers:

<p>int *ptr; = Declares a pointer variable that can store the address of an integer. &amp;variable = Retrieves the memory address of a variable. *ptr = Accesses the value stored at the memory address pointed to by <code>ptr</code>. NULL = Represents a pointer that does not point to any valid memory location.</p> Signup and view all the answers

Which rule should be followed when writing C?

<p>Never put function or variable definitions in a header file. (C)</p> Signup and view all the answers

In C, references will either refer to an invalid object/array or be null.

<p>False (B)</p> Signup and view all the answers

What is the output of this code int x = 10; int * y = &x; int z = *y;

<p>10</p> Signup and view all the answers

In C, a pointer is considered a(n) ______ and is interpreted as memory address.

<p>integer</p> Signup and view all the answers

Match the pointer with what it points to.

<p>int **vp; = Pointer to an Integer pointer void ***ouch; = Pointer to a pointer to a pointer to any kind of value</p> Signup and view all the answers

What is the output of this code? int x1 = 10, x2 = 20; int * p1; p1 = &x1; *p1 = 100;

<p>x1 = 100, x2 = 20 (D)</p> Signup and view all the answers

A variable of a primitive type stores the address of an object or array.

<p>False (B)</p> Signup and view all the answers

A variable of what type should be initiated, especially in C?

<p>Pointers</p> Signup and view all the answers

In C, a pointer is a variable that stores a(n) ______.

<p>memory address</p> Signup and view all the answers

Which of the following is the correct way to retrieve a memory address and apply a dereference operator

<p>ptr = &amp;var; = Retrieves the memory address *ptr = Apply the dereference operator</p> Signup and view all the answers

What is the output of this code? int x1 = 10 , x2 = 20; int * p1, * p2; p1 = &x1; p2 = &x2; * p2 = * p1

<p>Value of x1 =10, Value of x2 = 10 (B)</p> Signup and view all the answers

Function definitions should be declared and defined in header files.

<p>False (B)</p> Signup and view all the answers

What is the default value of local variables?

<p>Garbage</p> Signup and view all the answers

The address of the variable can be stored in a(n) ______.

<p>pointer</p> Signup and view all the answers

Match variable with variable type in java:

<p>Primitive variables = Instance variables All variable of class or array type = Reference variables</p> Signup and view all the answers

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

<p>0 (A)</p> Signup and view all the answers

A reference is manipulated by the program

<p>False (B)</p> Signup and view all the answers

How does Java handle memory compared to C?

<p>Java has no notion of memory while C has.</p> Signup and view all the answers

To get the value you need to ______ the pointer.

<p>dereference</p> Signup and view all the answers

How to access a variable of a struct.

<p>struct node start; = .operator struct node * pointer = -&gt; operator</p> Signup and view all the answers

Which of the following data types can a void * pointer point to?

<p>All of the above (D)</p> Signup and view all the answers

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.

<p>False (B)</p> Signup and view all the answers

Flashcards

Memory structure

Memory is structured as an array of bytes, each piece of data has an address.

Java types

Primitive types store the actual value, while reference types store the memory location of an object or array.

Pointers

C language feature that stores the memory address of a variable and can be manipulated.

Reference

Refers to an object or array, is implementation-dependent, and cannot be directly manipulated.

Signup and view all the flashcards

Pointer

A variable that stores a memory address and can be manipulated.

Signup and view all the flashcards

Java reference

Only stores a valid object reference or null and cannot store an arbitrary value.

Signup and view all the flashcards

C Pointer

An integer interpreted as a memory address that can store any arbitrary value.

Signup and view all the flashcards

Instance type in C

Variable stores the value itself.

Signup and view all the flashcards

Pointer types in C

Variable stores memory address of the value.

Signup and view all the flashcards

Address-of operator (&)

Returns the memory address of a variable.

Signup and view all the flashcards

Dereference operator (*)

Returns value at the memory address pointed to by pointer.

Signup and view all the flashcards

Reference

An address that can be modified.

Signup and view all the flashcards

C Array

Arrays are defined by specifying the array size after the name.

Signup and view all the flashcards

Strings in C

Arrays are characters ending with a null terminator ( ).

Signup and view all the flashcards

The '&' Symbol

When used as a binary operator, the ampersand is the bitwise AND operator.

Signup and view all the flashcards

Code organization in c

Never define functions or variables in header files, which can lead to multiple definition errors.

Signup and view all the flashcards

Pointer

Variable that stores memory addresses.

Signup and view all the flashcards

Dereference Operator

Access the content of memory pointed to by a pointer.

Signup and view all the flashcards

Pointers and arrays

Arrays support the same set of operations, but arrays can not be assigned new addresses.

Signup and view all the flashcards

Why Use Pointers?

Dynamic memory allocation, modifying function variables, and efficient data passing.

Signup and view all the flashcards

Pointer Definition

Defined using <variable type>* ptr_name;

Signup and view all the flashcards

Value Pointed To

Reads as "value pointed to by."

Signup and view all the flashcards

Printf statement

\nValue of x1 = 10\nValue of x2.

Signup and view all the flashcards

What Is converted?

Can be implicitly converted to pointer of proper type.

Signup and view all the flashcards

What is arithmetic?

Arithmetic operations are only addition and subtraction.

Signup and view all the flashcards

Passes by arguments?

Changes value of pointer argument.

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 variable x.
  • 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.

Quiz Team

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.

More Like This

Use Quizgecko on...
Browser
Browser