Lecture 4 References and Instances in Java
45 Questions
0 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 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.

False (B)

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.

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

Match the pointer declarations with what they point to:

<p>int *ptr; = Pointer to an integer double *ptr; = Pointer to a double char *ptr; = Pointer to a character void *ptr; = Pointer to any data type</p> Signup and view all the answers

What is a key difference between references in Java and pointers in C?

<p>Pointers in C can store arbitrary memory addresses, while Java references can only store valid object locations or null. (B)</p> Signup and view all the answers

In C, it is possible to perform arithmetic operations on pointers.

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

In the C programming language, what term describes converting a pointer back to the value it is pointing at?

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

In terms of the 'no duplicates' code through, the main goal of the rmdup program is to remove ______.

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

Match the following C pointer declarations with their descriptions:

<p>int *ptr = A pointer to an integer variable char *str = A pointer to a character (often used for strings) void *data = A generic pointer that can point to any data type int **ptr = A pointer to a pointer to an integer</p> Signup and view all the answers

In C, if you have a pointer int *ptr and you increment it using ptr++, what happens?

<p>The pointer becomes invalid. (B)</p> Signup and view all the answers

In C, arrays are always passed by value to functions.

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

In C, what is the term for a pointer that does not point to any valid memory location?

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

In C programs, function and variables definitions should not appears in ______ files.

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

Match the following concepts related to memory addresses:

<p>Address of Operator (&amp;) = Used to obtain the memory address of a variable. Dereference Operator (*) = Used to access the value stored at a memory address. Pointer = A variable that stores a memory address. Memory Address = The location in memory where data is stored.</p> Signup and view all the answers

In C, what is the significance of the asterisk (*) in the declaration int *ptr;?

<p>It declares a constant integer. (C)</p> Signup and view all the answers

In Java, all variables of primitive types are reference variables.

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

In the context of pointers, what does 'dereferencing' mean?

<p>accessing the value at the memory address stored in the pointer</p> Signup and view all the answers

In C, strings are stored in ______ arrays that are null-terminated.

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

Match the following operations with their corresponding effects on a pointer:

<p>ptr++ = Increments the pointer to point to the next element of its type in memory. *ptr = Accesses the value stored at the memory location pointed to by the pointer. ptr = &amp;variable = Assigns the address of 'variable' to the pointer. void *ptr = Declares a generic (untyped) pointer.</p> Signup and view all the answers

What is the purpose of the void * pointer type in C?

<p>It represents a pointer to a <code>void</code> variable. (C)</p> Signup and view all the answers

In C, arr = ptr; is valid if arr is an array and ptr is a pointer of the same type.

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

What is the primary use case for pointers?

<p>dynamically allocating memory</p> Signup and view all the answers

To access struct fields with an instance rather than a pointer utilize the ______ operator

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

Match the C pointer arithmetic operations with their descriptions:

<p>*p++ = Accesses the value pointed to by <code>p</code>, then increments <code>p</code>. *++p = Increments <code>p</code>, 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

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

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

Pointers can point to only variables and not arrays.

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

What is the default value of local and global variables?

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

In memory, each piece of ______ has a corresponding address.

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

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

<p>Value of x1 = 100 Value of x2 = 200</p> Signup and view all the answers

Given the following C code, what does the expression *ptr++ do?

int arr[] = {10, 20, 30};
int *ptr = arr;
// Expression: *ptr++

<p>Increments the pointer <code>ptr</code> and then accesses the value at the new memory location. (C)</p> Signup and view all the answers

A pointer can only point to variables of the same data type as the pointer itself.

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

Explain the significance of initializing all variables, and especially pointers, in C programming.

<p>avoids undefined behavior or garbage values</p> Signup and view all the answers

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

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

Associate operations and/or pointer arithmetic with their descriptions:

<p>Pointer Dereferencing = Retrieving the content stored at the memory address. Pointer Arithmetic = Performing arithmetical operations to access the current or immediate adjacent memory location. Address-Of Operator = Getting the address of the variable.</p> Signup and view all the answers

Which of the following statements about C arrays is correct?

<p>C arrays can be defined as local, static, or global. (C)</p> Signup and view all the answers

In C, the size of a pointer depends on the data type it points to.

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

Differentiate between the address of operator (&) and the dereference operator (*).

<p>address retrieves its memory address,dereference accesses value at specific memory location</p> Signup and view all the answers

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.

<p>*ptr</p> Signup and view all the answers

Match each term with corresponding description of a C Pointer:

<p>Dangling Pointer = Pointer pointing to a memory location that has been freed. Void Pointer = Pointer that can point to any data type in C. Null Pointer = Pointer does not refer to any valid memory address.</p> Signup and view all the answers

Given the C code snippet:

int x = 5;
int *p = &x;
*p = *p + 3;

What is the final value of x?

<p>3 (C)</p> Signup and view all the answers

In C, strings are mutable, meaning their contents can be changed after they are created.

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

What should you do with all variables, especially pointers, when programming in C?

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

In C, a string must be terminated by a ______ value.

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

In Java, what does the concept of references mean?

<p>Reference = a stored memory location of the object/array</p> Signup and view all the answers

Flashcards

Memory

A contiguous array of bytes in memory, each with a unique address.

Primitive Type

A type that directly holds a value (e.g., int, char).

Reference Type

A type that stores the location (address) of an object or array.

Java's Variable Types

Java doesn't allow choice, primitive types store data, classes/arrays store references.

Signup and view all the flashcards

Pointer (in C)

A variable that stores the memory address of another variable.

Signup and view all the flashcards

Reference (in C)

Refers to an object or array; implementation-dependent, not manipulated directly.

Signup and view all the flashcards

Pointer Manipulation

C allows manipulation of a pointer's value, referencing any memory address.

Signup and view all the flashcards

Instance Types in C

Variables store values directly; structs copy on assignment by default.

Signup and view all the flashcards

Pointer Types in C

Variables store memory ADDRESSES of values.

Signup and view all the flashcards

Arrow Operator (->)

Access struct fields through a pointer.

Signup and view all the flashcards

Struct (in C)

A composite data type; lacks methods unlike classes.

Signup and view all the flashcards

Address Of Operator (&)

Get the memory address of a variable.

Signup and view all the flashcards

Dereference Operator (*)

Get the VALUE at a memory address.

Signup and view all the flashcards

C Arrays

Contiguous memory locations of same type.

Signup and view all the flashcards

Array Name as a Reference / Pointer

Array name acts as pointer to the first element.

Signup and view all the flashcards

Strings in C

Arrays of characters, null-terminated.

Signup and view all the flashcards

What is a pointer?

Variable that stores memory address.

Signup and view all the flashcards

Address-of operator

Address-of: Finds variable address.

Signup and view all the flashcards

Defining a Pointer

Variable containing the memory address.

Signup and view all the flashcards

What is Dereference operator

Value at pointer's memory address.

Signup and view all the flashcards

Using Pointers in Functions

Changes the value at the memory location.

Signup and view all the flashcards

Pointer vs array

Array name can be pointer to proper type.

Signup and view all the flashcards

Pointer Arithmetic

Changes memory addresses and values.

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 bitwise AND 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
  • The pointer's definition needs to include the data type it is going to point to:
    • Example:
      • int x;
      • int *y;
      • y = &x;
  • 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 x
    • int 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.

Quiz Team

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.

More Like This

Use Quizgecko on...
Browser
Browser