Welcome, Guest. Please login or register.

Author Topic: assignment problem with general pointers (VOID type )  (Read 1266 times)

Description:

0 Members and 1 Guest are viewing this topic.

Offline Karlos

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16867
  • Country: gb
  • Thanked: 4 times
    • Show all replies
Re: assignment problem with general pointers (VOID type )
« on: January 04, 2005, 03:21:53 PM »
Quote

Einstein wrote:
Just a silly question as I'm not much at home in advanced C-programming: why does pointers in C need to be declared as certain types ? I figure this is a compiler-directive to have access to the different offsets in the varibales/structures/functions, am I correct ?  :-?


That's about it, really. At a machine level, pointers are just addresses. In the 680x0 for example, a pointer can be any 32-bit value. You usually have to care about the size of the element pointed to when modifying them.

In (slightly) higher level languages such as C, you define a pointer as pointing to a particular type. This can be anything , from a byte to a complex structure.

For example, assuming we have 32-bit integers;

int x;
int myInt[4] = { 0, 1, 2, 3 };
int *pInt = &myInt[1]; /* points to the second integer in the array */

C then takes care to correctly do the maths when you operations like

x = *(++pInt); /* x = 2 */
x = *(--pInt); /* x = 1 */
x = pInt[2]; /* x = 3 */

On a system with 32-bit integers and 32-bit pointers, the actual value of pInt would be changing by 4 when you use ++/-- and the index scaled by 4 when you use [].

Having typed pointers also helps to prevent many problems that could otherwise arise when dealing with structures etc.  The compiler can see the definition of a structure and can therefore ensure that the -> operation can only access the defined members of that structure.

The void pointer was introduced to deal with the requirement of uninitialised/undefined memory. The void pointer is purely designed to simply hold the address of something and does not allow dereferencing or arithmetic of any kind (since no information about the thing pointed to is known).
int p; // A
 

Offline Karlos

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16867
  • Country: gb
  • Thanked: 4 times
    • Show all replies
Re: assignment problem with general pointers (VOID type )
« Reply #1 on: January 07, 2005, 10:54:45 PM »
Quote

Jose wrote:
Hey, it just stroke me, that must also be why it's not permited to do pointer arithmetic with type void right ? Since the compiler doesn't know the size of the type the void * pointer points to it can't do pointer arithmetic.


Exactly. As I said, void poiters cannot have any arithmetic applied to them and cannot be dereferenced.

Their sole purpose is to allow a pointer that can "point to anything" as a handle to raw memory.
int p; // A
 

Offline Karlos

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16867
  • Country: gb
  • Thanked: 4 times
    • Show all replies
Re: assignment problem with general pointers (VOID type )
« Reply #2 on: January 08, 2005, 02:14:40 PM »
Quote

Jose wrote:
Ups, hadn't read that part... :-)


No, but you grasped it anyway, which is even better than someone telling you :-)
int p; // A