C Programming Flashcards
Pointers
For pointers “&” is the address operator
“*” is the dereference operator
Pointers provide a elegant of indirection to accessing program state,
A pointer is a variable that contains the address of a variable.
A pointer can only point to a object type(basic or dervied) suh as int, char, strcut, another pointer etc.
When delcaring a pointer it doesn’t point to anything unless initialized. Only memory is allocated
Dynamic memory allocation
allows a program to adjust to changes in sizes and space needs as it runs, allocating more space as it needs it and freeing up space it no longer needs
Program Scope
A variable’s scope defines when its name has meaning. In other words, scope defines the set of program code blocks in which a variable is bound to (associated with) a program memory location and can be used by program code.
Global Variables
variables that are declared outside the function body.
Global variables remain permanently in scope and can be used in any code in the program because they’re always bound to one specific memory location.
In C, global variables are saved in data segment
Advice: Global variables ….
avoid programming with global variables whenever possible.
Local variables
local variables are only in the scope inside the function in which they are defined.
Space to store a parameters value is allocated on the stack when the function gets called, and gets deallocated from the stack when the function returns.
Each activation of the function its own bindings for its parameters and local variables
For recursive calls, each call gets. a separate stack frame containing a space for its parameters and local variables.
Program address
a program’s address space represents storage locations for everything it needs for execution: storage for instruction and data.
Program’s address space can be thought of as an array of addressable bytes, each use of address in the program’s address space stores all or part of a program instruction or data value
Never dereference a NULL pointer
(*) is the dereference operator
“&” : is an address operator
“*” : is a dereference operator
example:
int *x;
int y =5;
x =&y;
printf(“%d”, *x);
A pointer can only point to one type (basic or derived)
Ex: int, char, a struct, another pointer, etc.
After declaring a point: int *ptr, ptr doesn’t actually point to anything yet. We an either make it point to something that already exists, or
allocate room in memory for something new.
array declaration
int ar[5]; // decare a 5 elemtn array
int arr[] = {78, 76}; //declares and fills a 2-elemtn array
int arr[5];// declares 5 eleent integer array
In C arrays are collections ocontinguous memory locations holding identical types
in java , arrays are collection of like items
accessing Arrays
arr[i]; returns the i-th element
index starts at 0
sizeof(ptr) gives the memory
len(arr) tells your size of array
how to acess poitners and dereference
let arr be an array
arr is like a pointer to first element of array
arr[0] is the same as arr[0]
arr[2] is the same as *(arr+2)
use pointers to pass arrays to functions
use pointer arithmetic to access arrays more conveniently
compiler is smart enough to identify type
ptr is just a memory address, we can add to the meory address to traverse an array
ptr + 1 will return pointer to next element
arr[20][40][60][ ][ ]
int * ptr;
ptr points to arr[0]
*ptr gives 20
ptr = ptr +1 ; this will give ptr ==104 which is the address of the next index not the value
what will (*ptr+1) do? what will *ptr++ do? what will *(ptr+1) do?
addr: 100. 104. 108 112 116
arr. [20]. [40]. [60]. [ ]. [ ]
what will (*ptr+1) do? 21
derference first then +1 to the value. 20+1
what will *ptr++ do? 20
post increment will give back a pointer, then defreerence first then adds 1 to the next address. So its will only increase theaddress after derefernce since a post increment.
what will *(ptr+1) do? 60
since ptr was changed to address 104 from last line, now its moves another 1 to 108 and then dereferences to 60.
*(ptr) ++ would give 20 without changing but 20 prints out but in a sense becomes 21 but without being printed.
int *p, arr[5]; declaration
p=arr+5;
arr=p+1;
Will any of the above create errors?
addr: 100. 104. 108 112 116
arr. [20] [ ] [ ] [ ] [ ]
p=arr+5;
will return garbage value, but no error.
will access the memory after 116 , which is garbage.
arr=p+1;
compiler error. type mismatch since arr is int array but p+1 is a int pointer
This will lead to errors . Illegal Lvalue
int * p, arr[5]; //delcaration
p=arr+5
*p=0;
what will happen?
Possible result in seg fault because the memory location being accessed may not be a valid address
int main (int argc, char **argv)
% ./a.out how are you?
what is *argv[3]?
*argv[3] is *(argv[3]) is ((argv+3))
all the above gives back a CHAR, single character
argv is [./aout ] [ out\0] [how\0] [are\0 ] [you?\0 ]
argv[3] gives back the first character of the 3rd string.
*argv[3] = ‘y’
Operator Legal
++, – , [], ==, != yes
Ptr + integer yes
Ptr - integer yes
integer - ptr NO (would be need to
negate pointer)
Pointers are always positive
Ptr – ptr Yes – returns an int
(distance in elements between pointers)
Ptr + ptr NO – not guaranteed to be an address
so K&R decided against
Ptr multiply ptr No - (just repeated addition).
…
*ptr=NULL;
pointers can be assigned NULL. Should not dereference bull pointers
do not dereference nnull pointer
int x = 10;
int * ptr;
ptr = &x;
*ptr=8;
what *ptr=8; does is changes the value of the address it is pointing at to 8. Dereferencing a pointer accesses the value that the pointer refers to.
ptr = &x;
this line initalizes tr to the address of x (ptr points to variable x)
int *ptr1, *ptr2, x, y;
x =8;
ptr2= &x; assigns ptr2 to address of x
ptr1=NULL;
*ptr2 = 10; memory location ptr2 points to its assigned 10
(changes the location ptr2 points to to the value 10.
y = *ptr2 +3; // y is assigne what ptr2 points to plus 3
ptr1 = ptr2; // ptr1 gets the address value stored in ptr2 (essentially both pointers are pointing to the variable x
*ptr1 = 100; // ptr1 changes data of the address its pointing to to 100. Now ptr2 is pointing to 100.
ptr1=&y;
*ptr1= 80;
now ptr1 has changed the value at y to 80.
y is whatever ptr2 points to and +3.
ptr2 i pointing to the address with a value of 10.
y= 10 + 3 = 13
ptr=20; // this assigns ptr to point to address 20 which is wrong.
ptr =&x;
*ptr=20; //this assigns 20 to the memory pointed to by ptr
ptr=NULL;
*ptr=6;
What will happen?
This will cause a crash as we are trying to modify something without assigning ptr to anything (seg fault)
.
Pointer Arithmetic
what does “ ptr + 1 “ return/give/do?
what does “ ptr++ “ return/give/do?
ptr+1 will return a pointer to the next element
ptr++ will move pointer to next index but after since its a post fix.
Pointers will not point to a value. Pointers hold a number that is a memory address. ITs just a value representing memory location. It “points” to your “residennce”
int * ptr = & x
This code will declare a variable ptr as a integer pointer and initilaized it to the address of where the variable x has been placed.
The following two statements are TRUE OR FALSE:
int*ptr = arr;
int * ptr = &arr[0]
True, the two lines are the same
The following two statements are TRUE OR FALSE:
int*ptr = arr[2];
int * ptr = *(arr+2)
True, the two lines are the same
what the the following statement return?
int val=2;
int* ptr = &val;
printf(“%p”, ptr);
It prints the address of where val is stored