100% finding sum of elements of array using pointer & function (ఎక్కువ వివరాలతో long answer type))

// Online C compiler to run C program online
#include <stdio.h>
int adding(int *p)
{
  int  i, total=0;
   printf("\n we r inside the function now");
   printf("\n initial address to which the pointer is pointing =%u",p);
  printf("\n initial value to which the pointer is pointing =%d",*p);
  for(i=0;i<3;i++)
  {
      total=total+*(p+i);
  }
   printf("\n sum of values in the array=%d",total);
   printf("\n finally, the address to which the pointer is pointing =%u",p+i);
   printf("\n we are about to leave the user-defined function and return to main");
  return total;
 }
int main()
{
 int *ptr,a[]={5,15,25},array_sum;
 ptr=&a[0];//pointing to 1st element of array
 printf("\n initial address to which the pointer is pointing =%u",ptr);
 printf("\n initial value to which the pointer is pointing =%d",*ptr);
 array_sum=adding(ptr);
 printf("\n we came back to main");
 printf("\n finally, the address to which the pointer is pointing =%u",ptr);
 printf("\n sum of elements in the array:%d",array_sum);
 return 0;
}

/*గమనిక ... ఈ ప్రోగ్రామ్ ని https://www.programiz.com/c-programming/online-compiler/ లో  రన్ చేసినప్పుడు ఒకో integer నాలుగు బైట్లు తీసుకున్నట్టు తెలుస్తున్నది.  Windows 7 (64 bit ) Turboc  లో చేసినప్పుడు integer  రెండు బైట్లు తీసుకున్నడా అని పరిశీలనా చేయగలరు */
తెరమీద 

/tmp/2xuzwkT4m6.o
initial address to which the pointer is pointing =2832325176
 initial value to which the pointer is pointing =5
 we r inside the function now
 initial address to which the pointer is pointing =2832325176
 initial value to which the pointer is pointing =5
 sum of values in the array=45
 finally, the address to which the pointer is pointing =2832325188
 we are about to leave the user-defined function and return to main
we came back to main
 finally, the address to which the pointer is pointing =2832325176
 sum of elements in the array:45  

కామెంట్‌లు

ఈ బ్లాగ్ నుండి ప్రసిద్ధ పోస్ట్‌లు

100% C prog to write content into a file and then read it back onto the console

Spic macay

శ్రీరస్తు.. రామలింగ 18-1-24 (సూరసాని వారి ఆహ్వానము)