CSCI-1190: Beginning C Programming For Engineers: Lecture 4: Arrays and Pointers Gang Chen
CSCI-1190: Beginning C Programming For Engineers: Lecture 4: Arrays and Pointers Gang Chen
a -= b; a = a – b;
a *= b; a = a * b;
a /= b; a = a / b;
a %= b; a = a % b;
Expressions as Statements
• Expressions can be directly used as
statements
1. 1 + 34;
2. x – 9;
3. i-1,j+2,k-3;
4. a = 5;
5. b = c = d = 5;
6. a = b = 2 * ( c = 5);
7. foo(bar);
Postfix Increment and Decrement
• Postfix increment and decrement operators
return the original value of the variable,
then increment or decrement the variable.
int a,b;
a = b = 10;
printf("%d\n",a++); /* 10 */
printf("%d\n",a); /* 11 */
printf("%d\n",b--); /* 10 */
printf("%d\n",b); /* 9 */
Prefix Increment and Decrement
• Prefix increment and decrement operators
increment or decrement the variable, then
return its resulting value.
int a,b;
a = b = 10;
printf("%d\n",++a); /* 11 */
printf("%d\n",a); /* 11 */
printf("%d\n",--b); /* 9 */
printf("%d\n",b); /* 9 */
Arrays
• An array can be viewed as a set of indexed
variables with the same name.
• Declared using the [] notation to indicate the
range/size of the array
• Elements are indexed using the [] notation,
ranging from 0 to one less than the bound
a[0] a[1] a[2] a[3] a[4] i
int a[5], i;
for(i=0;i<5;i++){
a[i]=i;
}
Using Arrays
• There are no built-in operators for arrays
int i,a[5],b[5],c[5];
a=1; /* error */
b=2; /* error */
for(i=0;i<5;i++)
{ a[i]=1; b[i]=2; } /* ok */
c=a+b; /* error */
for(i=0;i<5;i++)
c[i]=a[i]+b[i]; /* ok */
Array Indices
• Indices must be integers
int i,j,a[10];
float r=1.0;
a[-1]=1; /* error */
a[10]=1; /* error */
a[15]=1; /* error */
a[r]=1; /* error */
j=r; /* j=1 */
a[j]=1; /* okay */
Multidimensional Arrays
int i,j,a[5][3];
for(i=0;i<5;i++)
{
for(j=0;j<3;j++)
a[i][j]=i*j;
}
In-Class Exercise 4-1
• Generate 10000 floating point random numbers between 0
and 1. Divide the range [0,1] into 20 intervals with width
0.05. The first interval is [0,0.05], the second is
[0.05,0.10], ..., the last is [0.95,1.00]. Show the number of
visits to each interval. The output of you program should
look like:
0.000000-0.050000: 501
0.050000-0.100000: 463
0.100000-0.150000: 476
...
0.900000-0.950000: 466
0.950000-1.000000: 493
Pointers
• Pointers are variables that contain memory
addresses as their values
1. int *p;
2. int i=1,j=2;
3. p=&i;
4. printf("*p=%d\n",*p); /* *p=1 */
5. p=&j;
6. printf("*p=%d\n",*p); /* *p=2 */
7. j++;
8. Printf(“*p=%d\n”,*p); /* *p=3 */
Arrays and Pointers
• In many cases arrays and pointers are
interchangeable
3000 3004 3008 3012 3016
1. int a[5];
2. int *p; a[0] a[1] a[2] a[3] a[4]
3. p=a;
4. p=&a[0];
5. p++;
6. p+=2;
7. *p=3; /* same as a[3]=3 */
8. *a=0; /* same as a[0]=0 */
9. *(a+2)=2; /* same as a[2]=2 */
10. p[1]=4; /* same as a[4]=4 */
11. printf("p-a=%d\n",p-a); /* p-a=2 */
12. a=p; /* error */
Pointers as Function Parameters
void increment1(int a){
a++;
}
void increment2(int* p){
(*p)++;
}
int main(){
int i=1;
increment1(i);
printf("i=%d\n",i); /* i=1, not 2 */
increment2(&i);
printf("i=%d\n",i); /* i=2 */
}
Arrays as Function Parameters
int sum(int a[], int n) /* equivalent to int sum(int* a, int n) */
{
int i,s;
for(i=0,sum=0;i<n;i++)
s+=a[n];
return s;
}
int main()
{
int i,c[100];
for(i=0;i<100;i++)c[i]=i;
printf("The total is %d\n",sum(c,100));
return 0;
}
In-Class Exercise 4-2
• The following incomplete program tries to swap two numbers by
calling a function named swap. Complete the definition of the swap
function and the function call.
#include <stdio.h>
int main()
{
int a,b;
printf("please input two numbers:");
scanf("%d %d",&a,&b);
printf("a=%d,b=%d\n",a,b);
swap();
printf("a=%d,b=%d\n",a,b);
return 0;
}