开发者

How do I make sure that I understand C pointers? [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 11 years ago.

Without any mentor, self-teaching myself with books and references on the web, how do I make sure that I properl开发者_StackOverflowy understand pointers in C? What tests should I go through, what questions to answer to be sure?


Try to answer questions about pointers here on Stack Overflow. This way you will find out very quickly if your understanding of pointers is correct or if other people disagree with the way you answered those questions.


In K&R there are exercises that you can solve to see if you got it right. Also, you can look for some questions posted with solutions from various university (and not only university) C programming classes, and solve them (then compare to the solutions).


You'll also need malloc and structs for these:

  • Implement singly and doubly linked lists.
  • Implement a binary tree.
  • Re-implement the qsort interface from stdlib using a sort of your choice. (Takes an array of arbitrary length, with arbitrary size arguments and a pointer to a function that acts on them)
  • Implement a stack ADT. (structs and pointers) You'll need to know malloc and structs.

stack.h

typedef struct stack *Stack;

// returns a Stack (struct stack*) for the other functions to use.
Stack createStack();

// put an item on the stack.
void push(Stack s, int value);

// remove an item from the top of the stack and return it.
int pop(Stack s);

// returns 0 if items are on the stack, non-zero otherwise
int isEmpty(Stack s);

// no memory leaks.
void destroyStack(Stack s);


Test like This and This will give score your syntax knowledge, but properly use of pointers requires far more than syntax understanding. Requires expertise only time will give you.

I would suggest you to practice. Implement memory managers (at the beginning simple) then look for Open Source project and see how they deal with pointers management.

I like this explanation, pretty simple.

AND of all the courses I have seen THIS ONE from MIT is very realistic and is open plus free (:


Explain why this code will work on some systems but not others, and what the condition is for whether it works.

int next_arg(va_list *ap)
{
    return va_arg(*ap, int);
}

int vsum(va_list ap)
{
    int x, y;
    for (x=0; y=next_arg(&ap); x+=y);
    return x;
}

int sum(int dummy, ...)
{
    int x;
    va_list ap;
    va_start(ap, dummy);
    x = vsum(ap);
    va_end(ap);
    return x;
}

Hint: If it's going to fail, it will be a compile-time error.


I see no direct answer for this.

Just write a bunch of test code that deals with pointers and verify that everything works as you expect it to. Use the debugger when you aren't sure.


Try a pointer trace. For example, problem 2 from here (solutions).

This is technically in C++, but all of the pointer parts of the question are in C.


Some "bread and butter" uses of pointers that you would need to understand:

  • Represent an array of data.
  • Point to specific element of array (for enumeration).
  • Pass reference of data to function so that function can alter original variable.
  • Access dynamically allocated memory (malloc for C or new for C++)


One key to understanding pointers is understanding the underlying computer architecture. What assembly language is and how it works. How basic processors handle instructions. What stacks and heaps are. How the call stack works. How memory allocation works.


Try to explain it to someone who isn't a programmer. If you can explain the pointers, then you understand it. If you get confused explaining it, you don't get it yet.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜