Lecture Notes: Pointers, Memory, and Strings in C

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/19

flashcard set

Earn XP

Description and Tags

Flashcards covering pointers, memory regions (stack vs. heap), struct access, and string handling from the Week 2 CSC lecture.

Last updated 12:18 AM on 6/18/26
Name
Mastery
Learn
Test
Matching
Spaced
Call with Kai

No analytics yet

Send a link to your students to track their progress

20 Terms

1
New cards

What are the two parameters passed to the main function in C?

The parameters are int argc, which is the count of arguments, and char **argv (or char *argv[]), which is the argument vector representing an array of strings.

2
New cards

What is the first element of the argv array (argv[0]) always assigned to?

The first element argv[0] is the name of the program that is being run.

3
New cards

Why is strtol() often preferred over atoi() for converting strings to integers?

strtol() allows the programmer to check if the string contained any digits and if there were any remaining characters after the number, providing better error checking.

4
New cards

In C, what does a pointer represent?

A pointer is a value that represents a memory address indicating where a piece of data can be found.

5
New cards

How do you acquire the memory address of a variable in C?

You use the ampersand operator &, which is the "address of" operator (e.g., &var).

6
New cards

What is the purpose of the dereferencing operator *?

The dereferencing operator is used to follow a memory address to access or modify the actual value stored at that location.

7
New cards

What does malloc() return, and what does this return type signify?

malloc() returns a void * (void pointer), which is a generic pointer to any type and should be cast to the specific type intended for use.

8
New cards

What is a segmentation fault?

A segmentation fault occurs when a program attempts to access a memory location it does not have permission to access, such as address 0 (null).

9
New cards

What are the primary differences between memory on the stack versus memory on the heap?

Stack memory is used for local function variables and is reclaimed once a function returns; heap memory is allocated dynamically via malloc(), is persistent for the lifetime of the program, and typically offers much more space (gigabytes vs. megabytes).

10
New cards

What is a memory leak?

A memory leak occurs when a program allocates memory on the heap but loses access to the pointer before calling free(), making that memory unreachable until the program terminates.

11
New cards

Why is the sizeof() operator used in conjunction with malloc()?

sizeof() determines the number of bytes for a specific type (e.g., sizeof(int)), ensuring malloc() allocates the correct amount of memory regardless of the system's architecture.

12
New cards

If an integer is 44 bytes and p is an int * pointing to address 10241024, what is the address of p + 1?

The address would be 10281028, because C automatically increments pointers in multiples of the size of the underlying data type.

13
New cards

What special character must terminate every valid string in C?

Every valid string must end with the null terminator, represented as \0 (which is numeric value 00).

14
New cards

When using strlen() to find the length of a string, how is the null terminator handled?

strlen() counts the characters in the string up to, but not including, the null terminator.

15
New cards

Under what circumstances should memmove() be used instead of memcpy()?

memmove() should be used when the source and destination memory buffers overlap, as it safely handles the transfer using a temporary buffer.

16
New cards

How does C pass parameters to functions?

All C function parameters are passed by value, meaning the function receives a copy of the argument's value rather than the original variable itself.

17
New cards

In the declaration int *x, y;, what are the resulting types of x and y?

x is a pointer to an integer (int *), but y is a standard integer (int), because the * symbol only applies to the variable directly to its right.

18
New cards

What is the benefit of using the arrow operator -> when working with structs?

The arrow operator -> is a shortcut that dereferences a pointer to a struct and then accesses its member (e.g., p->length is equivalent to (*p).length).

19
New cards

What happens to memory freed via the free() function?

The memory is released back to the system, and any pointers still holding that address become invalid; using them again causes a "use after free" bug.

20
New cards

What is the significance of the NULL macro in C?

NULL is effectively zero and is used to represent a pointer that does not point to any valid memory address, serving as a safe way to initialize or check pointers.