Sei sulla pagina 1di 2

Arrays Passing Arrays as Function Arguments

type arrayName[arraySize]; //declaration returnType myFunction(int *param); //formal parameters as a pointer


type arrayName[i]; // initialization returnType myFunction(int *param[10]); // formal parameters as sized array
x[0] = num1; //initialization at first location returnType myFucntion(int param[ ]); //form parameters as an unsized array
x[1] = num2; double getAverage(int arr[], int size); int *getRandom( ); OUTPUT:
Syntax 1: type arrayName[n] = {x0, x1, … , xk} where k = n-1 double getAverage(int arr[], int size) { int *getRandom( ) { r[0] = 123
Syntax 2: type arrayName[ ] = {x0, x1, … , xk} where k = n-1 int i;
double avg; static int r[10]; r[1] = 456
type newVariable = arrayName[ith value desired element];
double sum=0; int i; r[2] = 789
ex. double balance[3]; //declaration r[3] = 987
double balance[ ]; = {num1, num2, num3}; for (i=0; i<size;++i) srand( (unsigned)time( NULL ) ); r[4] = 654
//or for assigning a single element of the array sum+=arr[i]; r[5] = 321
for (i=0; i<10; ++i) {
double balance[2] = num3; r[6] = 111
avg = sum/size; r[i] = rand();
//accessing array elements OUTPUT: r[7] = 222
printf(“r[%d] = %d\n”, i, r[i]);
ex. double salary = balance[9]; Average value is:
}
return avg; r[8] = 333
//this will take the 10th element from the 214.400000
}
array and assign the value to the salar
return r;
Return Array r[9] = 444
variable. *(p+0) : 123
#include <stdio.h> from a
}
*(p+1) : 123
#include <stdio.h>
int main() { Function *(p+2) : 123
int main() { OUTPUT:
int balance[5]; #include <stdio.h> *(p+3) : 123
Element[0] = 100 double avg; int main() { *(p+4) : 123
int n[10]; Element[1] = 101
*(p+5) : 123
Element[2] = 102 avg = getAverage(balance,5); int *p;
for (int i=0; i<10; i++) { *(p+6) : 123
int i;
n[i] = i+100; *(p+7) : 123
printf(“Average value is: %f “,
} etc.
avg); p = getRandom( ); *(p+8) : 123
*(p+9) : 123
for (int j=0; j<10; j++) {
printf(“Element [%d] = %d\n”, j, n[j] );
Pointer to anreturn
Array0; in C for (i=0; i<10; i++)
} printf(“*(p + %d) : %d\n”, i, *(p+i));
} double balance[50];
return 0;
return 0; //balance is a pointer to &balance[0]
}
//&balance[0] is the address of the first element of
} the array balance.

double *p; // p is a pointer to double (able to store the address of a variable of


2D Arrays type double)
double balance[10];
type arrayName[ i ][ j ]; //declaration of a 2D array with i rows
and j columns /* once address is in p, *p will give us the
p = balance; // p is address of the first value available at the address stored in p.
type newVariable = arrayName[ithDesiredVal][jthDesiredVal]; element of balance;
ex. int threeDim[5][10][4] #include <stdio.h> OUTPUT
//declaration for a 3D array
Array values using pointer
int main() {
ex. int a[3][4] = { *(p + 0) : 1000.000000
{0, 1, 2, 3} /* initializers for row indexed by 0 */ double balance[5] = { 1000.0, 2.0, 3.4, 17.0, 50.0 }; *(p + 1) : 2.000000
{4, 5, 6, 7} /* initializers for row indexed by 1 */ double *p; //p is a pointer to double *(p + 2) : 3.400000
{8, 9, 10, 11} /* initializers for row indexed by 2 */ int i; *(p + 3) : 17.000000
}; *(p + 4) : 50.000000
EQUIVALENT TO printf(“Array values using pointer\n”);
OUTPUT:
int a[3][4] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}; Average value is:
for (i=0;i<5;i++) *(balance + 0) : 1000.000000
214.400000
int val = a[2][3]; //accessing 4th element from 3rd row of printf(“*(p + %d) : %f\n”, I, *(p+i) ); *(balance + 1) : 2.000000
the array. *(balance + 2) : 3.400000
printf(“Array values using balance as address\n”); *(balance + 3) : 17.000000
OUTPUT:
#include <stdio.h>
Average value is: *(balance + 4) : 50.000000
for (i=0; i<5; i++) {
214.400000
int main() { OUTPUT: printf(“*(balance + %d) : %f\n”, I, *(balance + i);
a[0][0] = 0
int a[5][2] = { {0,0}, {1,2}, {2,4}, {3,6}, {4,8}}; a[0][1] = 0 return 0; #include <stdio.h>
int I, j; } #include <string..h>
a[1][0] = 1 OUTPUT:
a[1][1] = 2 int main() { strcpy(str3, str1) : Hello
for (i=0; i<5; i++) {
strcpy(s1, s2); copies string s2 into string s1 strcat(str1, str2) :
a[2][0] = 2 char str1[12] = “Hello”;
for (j=0; j<2;j++) a[2][1] = 4 strcat(s1, s2); concatenates string s2 onto the end of char str2[12] = “World”; HelloWorld
string s1 char str3[12]; strlen(str1): 10
a[3][0] = 3
printf(“a[%d][%d] = %d\n”, i, j, a[i][j);
a[3][1] = 6 strlen(s1); returns the length of string s1 int len;
} //end i loop a[4][0] = 4
strcpy(str3, str1); //copy str1 into str3
} // end j loop a[4][1] = 8 strcmp(s1, s2); returns 0 if s1 and s2 are the same; less
printf(“strcpy( str3, str1) : %s\n”, str3);
than 0 if s1<s2; greater than 0 if s1 > s2

} //end main() strcat(str1, str2); //concatenates str1 and str2


strchr(s1, ch); returns a pointer to the first occurrence printf(“strcat( str1, str2) : %s\n”, str1);
Strings 1D array of characters terminated by ‘\0’ (null) of character ch in string s1

char arrayName[6] = { ‘H’, ‘e’, ‘l’, ‘l’, ‘o’, ‘\0’ }; len = strlen(str1); //total length of str after concatenation
strstr(s1, s2); returns a pointer to the first occurrence of printf(“strlen(str1) : %d\n”, len);
char arrayName[ ] = “Hello”; string s2 in string s1
return 0;
printf(“Greeting message: %s\n”, arrayName); }
Pointers •every variable is a memory location and every memory location has its address defined which can be accessed using ampersand (‘&’
denotes an address in memory) • a pointer is a variable whose value is the address of another variable (direct address of the memory location) • the
actual data type of all pointers regardless of type is a long
Usage
i. define pointer variable
type varName; //pointer declaration • the unary operator ’*’ is used to designate a variable as a pointer
ii. assign the address of a
variable to a pointer
int *integerPointer; integerPointer = &intVar;
double *doublePointer; doublePointer = &doubleVar;
iii. access the value at the
address available in the
float *floatPointer; floatPointer = &floatVar; pointer variable • ‘*’
char *charPointer; charPointer = &charVar; int *myFunction( ) {. . .} // declaring a function returning a pointer returns the value of the
#include <stdio.h> #include <stdio.h>
variable located at the
#include <string.h> address specified by its
const int MAX = 4; operand

Value of names [2] = Wednesday Addams


OUTPUT:
int main() {

Value of names [1] = Morticia Addams


Address of var variable: bffd8b3c

Value of names [3] = Pugsley Addams


Value of names[0] = Gomez Addams
Address stored in ip variable: bffd8b3c int main() {
int var = 20;
Value of *ip variable: 20
int *ip; char *names[ ] = {
“Gomez Addams”,
ip = &var; //store address of var in pointer variable “Morticia Addams”,
“Wednesday Addams”,
printf(“Address of var variable: %x\n”, &var); “Pugsley Addams”
printf(“Address stored in ip variable: %x\n”, ip); }

OUTPUT:
printf(“Value of *ip variable: %d\n”, *ip); int i=0;

return 0; for ( I = 0; I < MAX; i++) {


}
printf(“Address of var[%d ]: %s\n”, I, names[i]);
#include <stdio.h>
OUTPUT: } //end for loop
const int MAX = 3; Address of var[0] : bfdbcb20
return 0;
Value of var[0] = 10 }
int main() {
Address of var[1] : bfdbcb24
Value of var[1] = 100 void getSeconds(unsigned long *par); int *getRandom( );
int var[ ] = {10, 100, 200};
int I, *ptrInc; Address of var[2] : bfdbcb28 void getSeconds(unsigned long *par) { int *getRandom( ) { OUTPUT
int j, *ptrDec; Value of var[2] = 200 rand0
int k, *ptrCmp; /* get current number of seconds*/
static int r[10]; rand1
*par = time( NULL ); int i; rand2
ptrInc = var; // array address in pointer rand3
ptrDec = &var[MAX-1]; // array address of 1st element in pointer return;
j = 0; OUTPUT: srand( (unsigned)time(NULL)); rand4
ptrCmp = var; rand4
Number of seconds: 1294450468
}
k = 0; for (i=0; i<10; ++i) { rand5
#include <stdio.h> r[i] = rand(); rand6
for ( I = 0; I < MAX; i++) { #include <time.h> printf(“%d\n”, r[i]); rand7
} rand8
printf(“Address of var[%d ]: %x\n”, I, ptrInc);
printf(“Value of var[%d] = %d\n”, I, *ptrInc);
int main() { rand9
ptrInc++; return r; *(p + [0]) : rand0
unsigned long sec; } *(p + [1]) : rand1
} //end for loop for pointer incrementation getSeconds(&sec); *(p + [2]) : rand2
#include <stdio.h> *(p + [3]) : rand3
for (j = MAX; j > 0; j--) { #include <time.h>
printf(“Number of seconds: *(p + [4]) : rand4

printf(“Address of var[%d ]: %x\n”, j, ptrDec); %1d\n”, sec); int main() {


*(p + [5]) : rand5
int *p; *(p + [6]) : rand6
printf(“Value of var[%d] = %d\n”, j, *ptrDec);
ptrInc--; return 0; int I; *(p + [7]) : rand7
} *(p + [8]) : rand8
} //end for loop for pointer decrementation p = getRandom(); *(p + [9]) : rand9

while ( ptrCmp <= &var[MAX-1] ) { double getAverage(int *arr, int size); for (i=0; i<10; i++) {
double getAverage(int *arr, int size) { printf(“*(p + [%d]) : %d\n”, I, *(p+i));
printf(“Address of var[%d] = %x\n”, k, ptrCmp); }
printf(“Value of var[%d] = %d\n”, k, *ptrCmp); int i, sum=0;
ptrCmp++; return 0;
double avg;
k++; }

} //end while loop for pointer comparison for (i=0; i <size; ++i) {
sum += arr[i];
return 0; }
} #include <stdio.h>
avg = (double) sum/size;
int main() {
OUTPUT: OUTPUT: return avg;
Address of var[0] : bf882b30 Address of var[0] : bf882b38 } int balance[5] = {1000, 2, 3, 17, 50};
OUTPUT:
Value of var[0] = 10 Value of var[0] = 200 double avg; Average value is: 214.40000
Address of var[1] : bf882b34 Address of var[1] : bf882b34
Value of var[1] = 100 Value of var[1] = 100 avg = getAverage(balance, 5);
Address of var[2] : bf882b38 Address of var[2] : bf882b30
Value of var[2] = 200 Value of var[2] = 10
printf(“Average value is: %f\n”,
avg);
return 0;
}
Passing pointers to functions in C

Potrebbero piacerti anche