2 Replies - 205 Views - Last Post: 10 November 2012 - 06:21 AM Rate Topic: -----

#1 NeeZaaR  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 39
  • Joined: 29-August 12

Problem in Array and Pointer

Posted 10 November 2012 - 05:31 AM

How i can make the sum = &ptr?
to make the pointer and address for student[i]

#include<stdio.h>
void main()
{
	int *ptr;
	int i,sum,num=1;
	int students[]={6,4,8,3};
	for (i=0; i<4;i++)
	{
		printf("%d - The Value For Student is %d\n",num++,students[i]);
		sum=students[i];
		*ptr=sum;
		printf("%d - the value for Student[%d] is %d, has Pointer is %d and  has Address is %d \n",num++,i,students[i],*ptr,&ptr);
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Problem in Array and Pointer

#2 Xupicor  Icon User is offline

  • Nasal Demon
  • member icon

Reputation: 249
  • View blog
  • Posts: 582
  • Joined: 31-May 11

Re: Problem in Array and Pointer

Posted 10 November 2012 - 06:04 AM

First, that's int main! And do return 0 from main, to signify everything went ok. ;)

See printf docs, there's a special format specifier for addresses, it's %p.

I'm not sure what you want. Can you state your problem in a more specific manner?

Also - you don't really sum your values... And if you would - you'd get strange values due to uninitialized variable.
Was This Post Helpful? 1
  • +
  • -

#3 jimblumberg  Icon User is offline

  • member icon


Reputation: 3988
  • View blog
  • Posts: 12,301
  • Joined: 25-December 09

Re: Problem in Array and Pointer

Posted 10 November 2012 - 06:21 AM

You may also want to study the following links on Pointers, and Pointer abstraction and Arrays.

Here is a slight reworking of your program to show some of the different values:
#include<stdio.h>
int main()
{
   int *ptr;

   int i,sum,num=1;
   int students[]= {6,4,8,3};
   for (i=0; i<4; i++)
   {
      sum=students[i];
      ptr=&sum;
      printf("%d - The value contained in",num++);
      printf(" students[%d] is %d", i, students[i]);
      printf(" and the address is %p,", (void*)&students[i]);
      printf(" sum contains the value %d", sum);
      printf(" and the address of sum is %p,", (void*)&sum);
      printf(" the address of ptr is %p", (void*)ptr);
      printf(" and the value pointed to is %d\n", *ptr);
   }
}



Jim

This post has been edited by jimblumberg: 10 November 2012 - 06:21 AM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1