C Programming Pointers

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

What's the difference between "w" and "a" in fp = fopen("file.c","a") & fp = fopen("file.c","w")?

In "a", if a file exists, then the pointer points to the end of the file. If a file does not exist, it creates a new file. In "w", if a file exists it deletes the old file and creates a new file (overwrites it). If the file does not exist, it creates a new file. W+ is for writing and reading...

What's the difference between malloc(), calloc(), and realloc()?

Malloc() allocates uninitialized storage like an array. Calloc() allocates and initializes storage (all "O" for integer storage and all "NULL" for char storage). Realloc() dynamically resizes an existing buffer where contents of the old buffer are copied into the new buffer.

When opening a file, if the file does not exist, what value gets assigned to the associated file pointer?

NULL

Is the NULL symbolic constant when a pointer instantiation fails equal in value to zero or the null terminator, \0, which terminates a string?

No, it is a value of an address stored in a pointer variable

What does pointer math mean?

Pointer math is the process of when mathematically incrementing a pointer's value, one is actually traversing memory by a fixed data-size (1 byte size memory chunk for a char, 4 byte size memory chunk for an int or double)

Do all variables require a type, name, and a value?

True

Passing pointers to functions allow a function to return multiple values?

True

When pointers are declared in C, does the pointer need a data type specified?

Yes. Even in the case of allocating memory with malloc, a "void" data type is used. Another example: char a; a = 'A'; char* ptr; ptr = &a;

The calling function should pass what type of values if the receiving function receives a pointer?

char b, char** a, a= &b, then 'fun(a) -> fun(char** a)

When one prints out the address location with printf, in what format is the address written in?

hexadecimal notation

How would you allocate memory using calloc()?

int* ptr = (int*) calloc(desiredSize,sizeof(int))

For array a[] and pointer *p, how do you assign p to an element of array a?

p = &a[i] where i is the element location you desire or p=a+i where a is an array and i is the desired offset

For array a[] and pointer *p, to assess an array's elements value using a pointer you write?

p= a, *(p+i) where i is the desired offset

For array a[] and pointer *p, how do you assign p to the base of the array a?

p=a

How do you print out the size of memory allocated at a variable in C?

printf("Size of variable 'a' : %lu ", sizeof(a)); where %lu is for a long unsigned variable

When using malloc , void* chunk = malloc (1024), what does the 1024 stand for?

1024 bytes of memory to be allocated to the pointer chunk

In C programming, what is the free() used for?

To free up the allocated storage

What are the common symbolic constants that are commonly used when working with C pointers?

size_t & NULL: NULL- a pointer not assigned to any specific address, size_t which represents byte count (like the number of bytes in a buffer) when allocating memory

Why do pointers always have to be initialized before used?

An uninitialized pointer contains junk information as a default

When creating a buffer using malloc, char** input = (char*) malloc (sizeof(char)*size+1), what is the purpose of the extra +1?

The extra memory space is to hold the null terminator operator to let the computer know the string has terminated.

What do pointers represent?

The physical address location to where another variable stores its values

What are the 3 common operators used for pointers in C?

1. Unary asterisk (*) meaning "address of" 2. Unary ampersand (&) meaning "the value of" 3. Sizeof operator

When a pointer is initialized to NULL, what does that mean?

A pointer is not assigned to any specific address but no longer has junk values assigned to it

When opening a file with fp= fopen("file.c","r"), what does the fopen function return?

It returns the address of a file structure in memory, which is declared as pointer fp

What does the sizeof() operation return?

Returns the number of bytes a variable occupies in storage

After declared, what are we referring to when we write the pointer's name without an asterisk?

The address location to which the pointer is referencing

What does it mean when one writes an asterisk in front of a pointer any time after the pointer has been declared, e.g. *ptr?

The value of the memory space to which the pointer is pointing to

At the end of a program where you used malloc, you need to free the memory allocated?

True, free(ptr);

If you don't want to create a pointer in C, you can send the address of a variable to a function?

True, int a, fun(&a) -> fun(int* a)

How do you print out the address of a pointer in C?

printf("The address the pointer is pointing to: %p", ptr);

How do you print out the value of a pointer in C if the pointer is a char type?

printf("the value of a pointer: %c", *ptr);


Set pelajaran terkait

AP English Language Companion Journal Study Guide

View Set

Insurance Adjustor Exam Practice Questions

View Set

Sixth Extinction Crisis Plans and some animal phyla

View Set