5 Replies - 376 Views - Last Post: 15 February 2014 - 01:31 PM Rate Topic: -----

#1 knils  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 51
  • Joined: 20-June 13

why there is compile error?

Posted 26 January 2014 - 06:49 AM

#include<stdio.h>
int main()
{
 struct myStruct
 {
  int a;
 };
 void *ptr;
 struct myStruct myArray[10];
 ptr = myArray;
 (struct myStruct*)ptr++;
 myArray[1].a=10;
 printf("%d",(struct myStruct*)ptr->a);
 return 0;
}


Is This A Good Question/Topic? 0
  • +

Replies To: why there is compile error?

#2 salazar  Icon User is offline

  • D.I.C Addict

Reputation: 86
  • View blog
  • Posts: 538
  • Joined: 26-June 13

Re: why there is compile error?

Posted 26 January 2014 - 07:25 AM

What errors are you receiving?
Was This Post Helpful? 0
  • +
  • -

#3 sepp2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2015
  • View blog
  • Posts: 3,042
  • Joined: 21-June 11

Re: why there is compile error?

Posted 26 January 2014 - 07:30 AM

In the future, when asking about code that causes compilation errors, you should post the error message that you get. That makes it much easier for people to see what the error is.

That said, your errors are probably caused by these two precedence issues:

(struct myStruct*)ptr++;


This is parsed as (struct myStruct*)(ptr++);. That is, the ++ operator is called on ptr and then the result is casted. Since ptr is a void pointer, on which you cannot perform arithmetic, this causes a type error. What you presumably intended was to cast ptr and then use ++ on the result. For that you need more parentheses: ((struct myStruct*)ptr)++;.

[code](struct myStruct*)ptr->a[/il]

Same issue as above basically. You're trying to access the a member through a void pointer and then cast the result. Again parentheses are required.
Was This Post Helpful? 2
  • +
  • -

#4 knils  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 51
  • Joined: 20-June 13

Re: why there is compile error?

Posted 11 February 2014 - 11:04 AM

#include<stdio.h>
	int main()
	{
	 struct myStruct
	 {
	  int a;
	 };
	 void *ptr;
	 struct myStruct myArray[10];
	 ptr = myArray;
	 ((struct myStruct*)ptr)++;
	 myArray[1].a=10;
	 printf("%d",((struct myStruct*)ptr)->a);
	 return 0;
	}



error:

struct_void_ptr_cast.cpp: In function ‘int main()’:
struct_void_ptr_cast.cpp:11:24: error: lvalue required as increment operand

This post has been edited by knils: 11 February 2014 - 11:05 AM

Was This Post Helpful? 0
  • +
  • -

#5 sepp2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2015
  • View blog
  • Posts: 3,042
  • Joined: 21-June 11

Re: why there is compile error?

Posted 11 February 2014 - 02:52 PM

Sorry, my bad, the result of a cast is not an l-value, so you'll need to cast, + 1 and then assign back to ptr.
Was This Post Helpful? 0
  • +
  • -

#6 knils  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 51
  • Joined: 20-June 13

Re: why there is compile error?

Posted 15 February 2014 - 01:31 PM

int main()
{
 struct myStruct
 {
   int a;
 };
void *ptr;
struct myStruct myArray[10];
ptr = myArray;


struct myStruct* iptr;
iptr = ((struct myStruct*)ptr); //cant increment void pointer

iptr++;

myArray[1].a=10;
printf("%d\n",iptr->a);
return 0;
}




//Its working!!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1