pointers for beginners

B

ballpointpenthief

I've put off posting this for a few days but this is still giving me
grief:
I just starting learning standard C and I plan to learn properly.

I *think* I understand pointers, however after writing some code that
used an array of pointers, I found that I could quite easily achieve
the same result with normal arrays.

This suggests to me that I am missing something here.

Could someone suggest an exercise where using pointers would be
essential?

Nice one very much in advance, Matt
 
R

Robert Gamble

ballpointpenthief said:
I've put off posting this for a few days but this is still giving me
grief:
I just starting learning standard C and I plan to learn properly.

I *think* I understand pointers, however after writing some code that
used an array of pointers, I found that I could quite easily achieve
the same result with normal arrays.

This suggests to me that I am missing something here.

I would suggest reading sections 4, 5, and 6 of the comp.lang.c FAQ
available at http://www.eskimo.com/~scs/C-faq/top.html (You would
undoubtedly benefit from reading the entire FAQ but the sections I
listed are a good start for your situation)
Could someone suggest an exercise where using pointers would be
essential?

Storing the return value of malloc (or one of many other standard
functions that return a pointer). Unlike many other languages,
pointers play an integral role in C and a full understanding of how
they work is essential to realizing the potential of the language.

Robert Gamble
 
O

osmium

ballpointpenthief said:
Could someone suggest an exercise where using pointers would be
essential?

Write code for a linked list. You can find tutorial on the Web as to the
essentials you need to know.
 
B

Barry Schwarz

I've put off posting this for a few days but this is still giving me
grief:
I just starting learning standard C and I plan to learn properly.

I *think* I understand pointers, however after writing some code that
used an array of pointers, I found that I could quite easily achieve
the same result with normal arrays.

This suggests to me that I am missing something here.

Could someone suggest an exercise where using pointers would be
essential?

How would you handle dynamic memory allocation if the returned value
was not a pointer?

How would strtok tell you where the next token started if it did not
return a pointer?

Whenever an array is passed to a function, the actual value passed is
a pointer to the first element of the array.

When a function must return multiple values (e.g., strtol), the
address of the variables to receive these values are passed as
arguments and treated as pointers by the receiving function.


<<Remove the del for email>>
 
N

Nick Keighley

ballpointpenthief said:
I've put off posting this for a few days but this is still giving me
grief:
I just starting learning standard C and I plan to learn properly.

I *think* I understand pointers, however after writing some code that
used an array of pointers, I found that I could quite easily achieve
the same result with normal arrays.

This suggests to me that I am missing something here.

Could someone suggest an exercise where using pointers would be
essential?


/* a function to swap two variables */
void swap (int* a, int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}

int main (void)
{
int x = 1;
int y = 2;
swap (&x, &y);
return 0;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top