10 Replies - 463 Views - Last Post: 02 January 2013 - 02:00 PM Rate Topic: -----

#1 nkasei28  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 93
  • Joined: 10-May 09

how to assign address of int variable to an entry in array of pointers

Posted 02 January 2013 - 01:24 AM

Hi,

Why would this not work? And what is the right thing to do.
        int x,y,z;

	printf("Enter three integers: ");
	scanf("%d %d %d",&x,&y,&z);
        
	p[0]=&x; //syntax error here
	p[1]=&y; //and here
	p[2]=&z; //and here



Is This A Good Question/Topic? 0
  • +

Replies To: how to assign address of int variable to an entry in array of pointers

#2 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 1
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: how to assign address of int variable to an entry in array of pointers

Posted 02 January 2013 - 10:01 AM

Assuming your array is of the same type, just remove the '&' for your assignments.
Was This Post Helpful? 1
  • +
  • -

#3 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2153
  • View blog
  • Posts: 3,311
  • Joined: 21-June 11

Re: how to assign address of int variable to an entry in array of pointers

Posted 02 January 2013 - 01:04 PM

There is no way those lines would cause a syntax error unless there's a syntax error elsewhere in your file. They might cause a type error though if p is neither an array of pointers to int nor a pointer to pointers to int.

The right thing to do would depend on what exactly it is you're trying to do.
Was This Post Helpful? 0
  • +
  • -

#4 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 1
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: how to assign address of int variable to an entry in array of pointers

Posted 02 January 2013 - 01:23 PM

Yes it does Sepp2k.

Assuming an int array as I said before, you get a cannot convert from int* to int.
Was This Post Helpful? 0
  • +
  • -

#5 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2153
  • View blog
  • Posts: 3,311
  • Joined: 21-June 11

Re: how to assign address of int variable to an entry in array of pointers

Posted 02 January 2013 - 01:31 PM

View PostButchDean, on 02 January 2013 - 09:23 PM, said:

Yes it does Sepp2k.

Assuming an int array as I said before, you get a cannot convert from int* to int.


I'm not sure whether you're agreeing with me ("Yes, as you said, it does cause a type error if it's an int array") or disagreeing with me ("Yes, it does cause a syntax error").

In the latter case: not being able to convert from one type to another is not a syntax error - it's basically the definition of a type error.

In the former case: thank you for your support :-)
Was This Post Helpful? 0
  • +
  • -

#6 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 1
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: how to assign address of int variable to an entry in array of pointers

Posted 02 January 2013 - 01:34 PM

Oh you're talking about 'semantics'. I took it as you were saying there was no error at all when there was.
Was This Post Helpful? 0
  • +
  • -

#7 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2153
  • View blog
  • Posts: 3,311
  • Joined: 21-June 11

Re: how to assign address of int variable to an entry in array of pointers

Posted 02 January 2013 - 01:39 PM

I specifically said that there will be a type error if "p is neither an array of pointers to int nor a pointer to pointers to int". I don't see how that could be interpreted as "there is no error at all".

Anyway, misunderstanding cleared.
Was This Post Helpful? 0
  • +
  • -

#8 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3662
  • View blog
  • Posts: 11,463
  • Joined: 05-May 12

Re: how to assign address of int variable to an entry in array of pointers

Posted 02 January 2013 - 01:45 PM

Don't forget array of pointers to void. :)
Was This Post Helpful? 1
  • +
  • -

#9 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 1
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: how to assign address of int variable to an entry in array of pointers

Posted 02 January 2013 - 01:47 PM

Well, the fact that there was a misunderstanding means that there was scope for such. You said it 'might' cause a type error, there is no 'might' because it clearly does.

I didn't even run the code and I knew for sure there was an error.
Was This Post Helpful? 0
  • +
  • -

#10 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2153
  • View blog
  • Posts: 3,311
  • Joined: 21-June 11

Re: how to assign address of int variable to an entry in array of pointers

Posted 02 January 2013 - 01:51 PM

View PostButchDean, on 02 January 2013 - 09:47 PM, said:

You said it 'might' cause a type error, there is no 'might' because it clearly does.


If p is an array of int pointers, a pointer to int pointers or, as Skydiver pointed out, an array of or pointer to void pointers, then the code posted by the OP will not cause a type error (or any other type of error). That's why I said "might".
Was This Post Helpful? 0
  • +
  • -

#11 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 1
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: how to assign address of int variable to an entry in array of pointers

Posted 02 January 2013 - 02:00 PM

Then make it clear as I did. Simple really.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1