4
Contributors
3
Replies
24
Views
4 Years
Discussion Span
Last Post by rubberman
0

If you are writing something like an operating system, and you have a system structure located at a fixed location, say 0x123456, then it would make sense to cast the integer 0x123456 as a pointer.

Other than that, I can't think of any reason to do it.

0

Pointers to integers are often used in C programming when you want to set an external variable inside a function, and return some other value. Example:

typedef struct {
  int sizeofbuffer;
  char buffer[1];
} somestruct_t;

int getSizeOfData(somestruct_t* pStruct, int* pBufSize)
{
    int err = 0;
    if (pStruct == 0)
    {
        err = 1;
    }
    else if (pBufSize == 0)
    {
        err = 2;
    }
    else
    {
        *pBufSize = pStruct->sizeofbuffer;
    }
    return err;
}

int myfunc(somestruct_t* pStruct)
{
    int bufsize = 0;
    int err = getSizeOfData(pStruct, &bufsize);
    switch(err)
    {
        case 0: /* Got data */ printf("buffer size == %d\n", bufsize); break;
        case 1: /* Void pointer received */ break;
        case 2: /* Should not happen */ break;
        default: /* Unrecognized error value */ break;
    }
    return bufsize;
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.