Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and the Yahoo Answers website is now in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

Question about Pointers in C programming?

So I'm trying to understand pointers and so far I've gotten that a pointer is a variables that points to the memory address if another variable. That memery address is the pointers variables' value. Is that correct? So:

int a;

int *ptr;

ptr = &a;

Though I don't understand a few things. Firstly what is point in having a NULL pointer is it isn't going to point to anything? Secondly what are pointers generally used for when writing code? I've only been writing simple code so far. Could you further explain why and how pointers may be used (preferably in the simplest way to understand). Thanks!

2 Answers

Relevance
  • 7 years ago
    Favorite Answer

    The NULL pointer is always just 0. The address of 0 is never supposed to be used to make anything. The choice of 0 was because most compilers at the time would put code, if anything at all, at location 0. So it was a "safe" assumption that data would never be located there. Later, this was formalized so that neither functions nor data can be located exactly at 0, for C compilers. It was easy to do.

    At the time, it was felt that you need a special pointer to indicate "no valid pointer." You could separately return a status value, instead. But this would mean returning both a pointer and a status value. It was easier to set up the pointer concept so that included a special number that means "not valid" and just combine both ideas. In floating point, for example, there are special values for infinity and zero and "not a number" and other special error values. Similar concept. It's not hard to just stuff into your head that there is a special value that is reserved as meaning "invalid pointer" and that this value is exactly 0 all of the time. Everything else might be valid.

    Just a matter of agreed upon convention.

    Pointers can point to functions or to data. Keep in mind that the size of a pointer might be different depending upon what it points at. "void" pointers are supposed to be large enough that they can hold a pointer to any kind of data, so you can use them as a way of holding any kind of data pointer and not just one specialized kind.

    You might use a function pointer in a data table. For example, you might have a list of function names (say, SIN, COS, TAN, etc) which are expressed as strings, paralleled along with a list of functions to perform those computations. You would "look up" the name using the string, but then you would call the function implied by the string by calling indirectly through the table entry that holds a pointer to the function. The table might look like:

    struct {

        char *fname;

        double (*func)( double );

    } functions[]= { { "SIN", sine }, { "COS", cosine }, { "TAN", tangent } };

    Where sine(), cosine(), and tangent() are actual C functions you've also defined in the code. This allows you to just do a table search and not have to use a BIG switch() statement or if...else series and instead just use a simple for() loop and execute the function when you find the name.

  • Anonymous
    7 years ago

    NULL value is assigned to a pointer variable so it doesn't point to some random location when they are not initialized and when a pointer is no longer required(linked list delete). Pointers are used in many cases, I can give some examples.

    When you want to pass a variable to a function, normal arguments can pass a copy of its value not the variable itself, if we pass a pointer, variable itself is passed to the function, the changes made in the function to pointer will affect the actual variable.

    When you write call back methods, methods are efficiently addressed using function pointers than by using a function name. When we want to change function, we just need to change the pointer value, not rewrite the code to change function name.

Still have questions? Get your answers by asking now.