Pointers exercises

I searched through this forum and Google too but I haven't found any simple pointers-only exercises. I need them for better learning.
If someone had any I would appreciate it very much. Thanks.
Do these:

1) declare ex1 as pointer to char
2) declare ex2 as pointer to pointer to char
3) declare ex3 as array 10 of pointer to char
4) declare ex4 as pointer to array 30 of char
5) declare ex5 as array 10 of pointer to array 500 of char
6) declare ex6 as const pointer to int
7) declare ex7 as pointer to const int

Use this site after you do the above yourself, to check if you got it right.
http://www.cdecl.org/

Bookmark this site for learning more about reading declarations.
http://unixwiz.net/techtips/reading-cdecl.html

Aside from this I don't know what pointer exercises you're looking for. A pointer is simply a variable that holds a memory address.

Hmm, you want to do pointer arithmetic? That's just as pointless (pun intended) as doing integer addition exercises -- all you need to do is learn the rules once, then it's always the same.
Last edited on
Well, it would be easier for me to remember these rules if I keep doing exercises, it doesn't matter how pointless they are. I have trouble remembering and understanding things, so it really helps me
Last edited on
What do you have problems with, exactly?
Did you successfully solve the previous seven exercises? If yes, here's more:

8) write a function that takes a pointer to char named pc, and returns it
9) write a function that takes a pointer to int named pi, and returns it as a pointer to float
10) write a function that takes a pointer to int named pi, and verifies if the value pointed to by pi is odd or even, and displays a message saying which one it is

You might want to read this:
http://cplusplus.com/doc/tutorial/pointers/
Thank you for your help, catfish.
As English isn't my native language, usually tutorials like this are difficult for me to understand. Also, as I said, if I just read tutorials I will forget most of things after a while, so I usually do exercises which help me understand how everything works and it helps me to remember the usage of codes I am practicing with.
I'd also need some exercises where you could use pointers for various tasks
11) implement a dispatch table of three functions which print "Catfish", "chrisname" and "devonrevenge" and let the user choose which one to run by entering 1, 2 or 3

I would like to see your attempts on the previous three exercises.
Last edited on
As it's the first time I'm dealing with stuff like this, I'm pretty much confused. Before I continue doing other tasks I must know if I've got the right idea.
So.. is this what 8th task wants?

int blahblah (char *pc) {return (*pc);}
Last edited on
No. You made two mistakes:

1) the return type of the function is wrong -- it is not int

2) you apply the dereference operation to the pointer when you should not

When you declare a pointer, you do so by inserting an asterisk (*) between the type and the name:
1
2
3
// Type *Name;
int *i;
char *c;


Now, the asterisk changes its meaning to an operator, depending on the expression.
1
2
3
int product = 5 * 3; // multiplication operator

*p = 5; // dereference operator -- this is not a declaration of a variable 


You apply the dereference operator to pointers. When you dereference a pointer, you go to the data located at the memory address that pointer contains.

For instance:
1
2
3
4
5
6
7
int i = 5;
int *pi = &i; // & gets the memory address of i (reference operator)

// ( i ) is of type int
// ( &i ) is of type int *
// ( pi ) is of type int *
// ( *pi ) is of type int 


So the function in the end should be:
1
2
3
4
char * blahblah(char *pc) // return type is not int
{
    return pc; // do not dereference the pointer
}

Last edited on
> As it's the first time I'm dealing with stuff like this, I'm pretty much confused.

The declaration of a pointer is very straightforward; there is one and only one simple rule to remember:

If T is a type, then the type T* is 'pointer to an object of type T'.

There is nothing else that you have to remember.

For more complex types, just divide and conquer.

> 5) declare ex5 as array 10 of pointer to array 500 of char

1
2
3
4
using array_type = char[500] ; // type 'array_type' is 'array of 500 char'
using ptr_type = array_type* ; // type 'ptr_type' is 'pointer to 'array_type'
using ex5_type = ptr_type[10] ; // type 'ex5_type' is 'array of 10 ptr_type'
extern ex5_type ex5 ; // declaration of ex5 



> declare bar as const pointer to array 5 of pointer to function (int) returning const pointer to char
> (from the gibberish specialist ridicous_fish@http://www.cdecl.org/ ):

1
2
3
4
5
6
7
using pchar = char* ; // pointer to char
using const pchar function_type(int) ; // type of the function
using pfn_type = function_type* ; // type of the pointer to function
using array_type = pfn_type[5] ; // type of the array
using parray_type = array_type* ; // type of pointer to the array
using required_type = const parray_type ;
extern required_type bar ; // declaration of bar 


Answer from the C gibberish <-> English site:
char * const (*(* const bar)[5])(int )

Completely avoid this gibberish site like the plague; it just takes perverse delight in making a simple thing appear extremely complicated.


> So.. is this what 8th task wants?
> int blahblah (char *pc) {return (*pc);}

8) write a function that takes a pointer to char named pc, and returns it

If the 'it' in 'returns it' means 'return the value of the pointed char', then yes.

But I guess the 'it' in 'returns it' means returns the same pointer
char* blahblah( char *pc ) { return pc ) ;
Last edited on
9) write a function that takes a pointer to int named pi, and returns it as a pointer to float

Now I have trouble understanding this, how do I convert from int to float? Or don't I need to convert anything here?
An "easy" way to convert something to another thing is by casting.
C++ supports more than one kind of cast, but for now just use reinterpret_cast:

Example:
1
2
3
4
SourceType s;
TargetType t;

t = reinterpret_cast<TargetType> (s);

but for now just use reinterpret_cast:


avoid using reinterpret_cast, as it is far and away the most dangerous of all the [C++] casts.

The "normal" cast is static_cast... and until you understand the difference between all the casts, static_cast is the one you should use.
Last edited on
> 9) write a function that takes a pointer to int named pi, and returns it as a pointer to float

>> Now I have trouble understanding this


That is good; because you should not be even thinking of doing something like this. Why do you want to learn how to shoot yourself in the foot?

If you are curious, check out reinterpret_cast<>
http://en.cppreference.com/w/cpp/language/reinterpret_cast

And read the whole page, paying particular attention to the section on 'Type aliasing'.


I apologize if some of the exercises I proposed were uninspired, and may promote bad practices.

My intent is to help the OP understand pointers, and learn about C++ features.
> I apologize ...

Don't apologize for having made a genuine effort to help someone new to programming.


For uzferry:

Chapter 5 of K&R ("The C Programming Language", 2nd edition, Kernighan and Ritchie) has some excellent exercises on pointers; you should read the chapter (if you haven't done so already), attempt to find answers to the exercises. You could check your answers for some of the exercises with the ones here: http://users.powernet.co.uk/eton/kandr2/krx5.html

Pointer basics http://cslibrary.stanford.edu/106/ and the companion video http://cslibrary.stanford.edu/104/ from Stanford. This too has a few exercises.

A tutorial with a large number of code snippets: http://pweb.netcom.com/~tjensen/ptr/pointers.htm

Another, with pictures, diagrams and code: http://www.augustcouncil.com/~tgibson/tutorial/ptr.html



Topic archived. No new replies allowed.