Abstract data type in c arrays in c
Download
1 / 9

ARRAYS - PowerPoint PPT Presentation


  • 140 Views
  • Uploaded on

Abstract Data Type in C , Arrays in C. ARRAYS. The Abstract Data Type. Array is a “consecutive set of memory locations”. Here we consider array as ADT. Here more concerned with the operation that can be performed on an array. Most language provide only two std. operations for arrays-

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about ' ARRAYS' - palma


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

The abstract data type
The Abstract Data Type

  • Array is a “consecutive set of memory locations”.

  • Here we consider array as ADT.

  • Here more concerned with the operation that can be performed on an array.

  • Most language provide only two std. operations for arrays-

    • Retrieves a value

    • Store a value


Definition
Definition

Abstract Data Type array

Return the value associated with the index if the index is valid , or an error if the index is invalid. Store accepts an array , in index , an item ,and return the original array augmented with the new <index ,value > pair.


Arrays in c
Arrays in C

  • A one-dimensional array is declared implicitly by appending brackets to the name of a variable

  • E.g.:- int list[5], *plist[5];

  • It declares two arrays each containing 5 elements

  • 1st defines array is 5 integers,2nd defines 5 pointers to integers.

  • In c, array index starts at 0,it may has list[0],list[1],list[2],list[3] and list[4](abbrevated list[0:4]


Cont….

  • The address of the first element list[0],is called the base address.

  • The size of int is denoted by sizeof(int) so the memory address of list[i] is

    α+i*sizeof(int).

    Alpha(α) is base address

  • When we write list[i] in c program , c interprets it as a pointer to an integer whose address is

    α+i*sizeof(int).


Cont..

Observe the diffrence between declaration

  • int *list1;

  • int list2[5];

  • list1 and list2 both pointers to an int, but in the second case 5 memory locations are reserved.

  • List2 is a pointer to list2[0] and list2 +i is a pointer to list2[i].

  • In C we do not multiply offset i with the size of the type to get to the appropriate element of array.

  • So in the case of array list[2] it is always the case that (list2 +i) equals &list2[i].So *(list2 +i) equals list2[i]


Program
Program…

#define MAX_SIZE 100

float sum(float [], int);

float input[MAX_SIZE], answer;

Void main (void)

{

inti;

for(i=0;i<MAX_SIZE;i++)

input[i]=i;

answer=sum(input,MAX_SIZE);

printf(“The sum is :%f\n”,answer);

}

float sum(float list[],int n)

{ inti; float tempsum=0;

for(i=0;i<n;i++)

tempsum+=list[i];

return tempsum;

}


Description of program
Description of program

  • When sum() is invoked input=&input[0] is copied into a temporary location and associated with the formal parameter list.

  • When list[i] occurs on the right hand side of the equal sign, a de-reference takes place and the value pointed at by (list+i) is returned.

  • If the list[i] occurs on the left hand side of the equal sign, then the value produced on the right hand side is stored in the location (list+i).

  • Thus in C array parameters have their values altered,despite the fact that the parameter passing is done by call-by-value.



ad