






































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







































Alexander Nelson September 9, 2019
University of Arkansas - Department of Computer Science and Computer Engineering
Arrays, Strings, & Pointers are all connected in C
Pointer – Variable that points to a memory location Requires:
Variable address accessed through ampersand (&) operator Literally the “address of” the variable Example:
int val = 5; int* myPtr = &val; //myPtr now is equal to memory location of val
int val = 10; int* myPtr = &val; //declare pointer to val *myPtr = 5; //dereference pointer and set //that location = 5 printf("val =%d",val);//print val=(value of val)
Output =?
Array – “an ordered series or arrangement” C Array – Block of memory storing associated variables
double height[10];
Array identifier stores address of first element Array identifier dereferenced by bracket operator obtains value
1st element stored at position 0, 2nd at 1, nth at (n-1)th element e.g. a[n] = (n+1)th element
Accessed with a dereference operator and offset with [] operator Can be read & modified like a normal variable
Valid Examples:
Multidimensional arrays declared with each dimension e.g. int array[3][2];
Initialize each column through comma separated curly brace statements e.g. int array[3][2] = {{1,2},{3,4},{5,6}};
Memory-wise, the above is equivalent to: int array[6] = {1,2,3,4,5,6}
Array identifier = pointer to first element Identifier (memory address) is passed by value = reference to array
Array elements may be accessed by offset (known type) Length of array is not known! Use delimiter or pass length parameter(s)
Array elements are passed by value! Original memory location will not be modified Example:
void myFunction(int a){ a = a+1; return a; } int main(){ int array[5] = {0}; myFunction(array[0]); printf("%d",array[0]); //Will print 0! }