Lecture – Arrays

The Development of the C Language*
https://www.bell-labs.com/usr/dmr/www/chist.html

It is interesting that one of the key design aspects of the language was representation of pointers and by extension arrays.

Ryan Robucci

Table of Contents

Review

Arrays in C

Declaration of Arrays

Examples

Array Implementation in C

Initialization

Mixing Array and non-array declaration/definition/initialization syntax

For the adventurous, you can mix array/non-array initialized/not-initialled:

int a[]={1,2}, i,k=0;

Accessing Array Elements

Char Array

scanf()

in general avoid scanf() with %s -- it is dangerous

use of scanf and %s is sufficient at first for learning C, but safer alternatives that avoid buffer-overruns will be explained in a later lecture

Example Program

#include <stdio.h>
int main(){
  char string1[11]="^^^^^^^^^^", string2[]="string";
  int i;
  printf("Enter a string: ");
  scanf("%s", string1);
  printf("string1 is: %s\nstring2 is: %s\n",
              string1, string2);
  for(i=0;string1[i]!='\0';i++)
    printf("%c",string1[i]);
  printf("$\n");
  for(i=0;i<11;i++)
    printf("[%2d] 0x%2x, %c\n",i,string1[i],string1[i]);
  printf("\n");
  return 0;
}
• include for printf/scanf



• prompt
• getting input using scanf
• printing strings using printf

• printing by iteration
    through char array
Enter a string: Hello there
string1 is: Hello
string2 is: string
Hello$
[ 0] 0x48, H
[ 1] 0x65, e
[ 2] 0x6c, l
[ 3] 0x6c, l
[ 4] 0x6f, o
[ 5] 0x 0, 
[ 6] 0x5e, ^
[ 7] 0x5e, ^
[ 8] 0x5e, ^
[ 9] 0x5e, ^
[10] 0x 0, 
 
 
 
 
 
 
 
 
 
•NULL-termination from scanf
 
 
 
 
•NULL-termination from intialization

Multidimensional Arrays

Passing Arrays to Functions

#include <stdio.h>
void ChangeElement(int array[ ], int index, int value){
 array[index]=value;
}

void main(){
    int arr [ ]={10,20};
    ChangeElement(arr,0,99); //a becomes 99,20
    for(int i=0;i<2;i++)
        printf("[%2d]: %2d\n",i,arr[i]);
}
▶︎
all
running...
[ 0]: 99
[ 1]: 20
[ 0]: 99
[ 1]: 20

Passing Array Elements

Protecting Array Elements

#include <stdio.h>
void ChangeElement(int array[ ], int index, int value){
 array[index]=value;
}

void main(){
    const int arr [ ]={10,20};
    ChangeElement(arr,0,99); //a becomes 99,20
    for(int i=0;i<2;i++)
        printf("[%2d]: %2d\n",i,arr[i]);
}
▶︎
all
running...
/tank/robucci/GIT/cmpe311/Lectures/CBasics/9qyqvrxn0_code_chunk: In function ‘main’:
/tank/robucci/GIT/cmpe311/Lectures/CBasics/9qyqvrxn0_code_chunk:8:19: warning: passing argument 1 of ‘ChangeElement’ discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]
    8 |     ChangeElement(arr,0,99); //a becomes 99,20
      |                   ^~~
/tank/robucci/GIT/cmpe311/Lectures/CBasics/9qyqvrxn0_code_chunk:2:24: note: expected ‘int *’ but argument is of type ‘const int *’
    2 | void ChangeElement(int array[ ], int index, int value){
      |                    ~~~~^~~~~~~~
[ 0]: 99
[ 1]: 20
code_chunk.c: In function ‘main’:
code_chunk.c:8:19: warning: passing argument 1 of ‘ChangeElement’ discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]
    8 |     ChangeElement(arr,0,99); //a becomes 99,20
      |                   ^~~
code_chunk.c:2:24: note: expected ‘int *’ but argument is of type ‘const int *’
    2 | void ChangeElement(int array[ ], int index, int value){
      |                    ~~~~^~~~~~~~

Warning is based on the argument passing, not the actual operations in the code.
In the following function, the array is not modified yet the same warning is generated

#include <stdio.h>
int ReadElement(int array[ ], int index){
 return array[index];
}

void main(){
    const int arr [ ]={10,20};
    printf("arr[0]: %d",ReadElement(arr,0));
}
▶︎
all
running...
/tank/robucci/GIT/cmpe311/Lectures/CBasics/ycjdjwqas_code_chunk: In function ‘main’:
/tank/robucci/GIT/cmpe311/Lectures/CBasics/ycjdjwqas_code_chunk:8:37: warning: passing argument 1 of ‘ReadElement’ discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]
    8 |     printf("arr[0]: %d",ReadElement(arr,0));
      |                                     ^~~
/tank/robucci/GIT/cmpe311/Lectures/CBasics/ycjdjwqas_code_chunk:2:21: note: expected ‘int *’ but argument is of type ‘const int *’
    2 | int ReadElement(int array[ ], int index){
      |                 ~~~~^~~~~~~~
[ 0]: 99
[ 1]: 20
code_chunk.c: In function ‘main’:
code_chunk.c:8:31: warning: passing argument 1 of ReadElement discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]
    8 |     printf("%d",ReadElement(arr,0));
      |                               ^~~
code_chunk.c:2:23: note: expected ‘int *’ but argument is of type ‘const int *’
    2 | int ReadElement(int array[ ], int index){
      |                   ~~~~^~~~~~~~
arr[0]: 10

Function with Const Array

#include <stdio.h>
int ReadElement(const int array[ ], int index){
 return array[index];
}

void main(){
    const int arr [ ]={10,20};
    printf("arr[0]: %d",ReadElement(arr,0));
}
▶︎
all
running...
[ 0]: 99
[ 1]: 20
arr[0]: 10