What's the position of pointers

Y

Yee.Chuang

When I began to learn C, My teacher told me that pointer is the most
difficult part of C, it makes me afraid of it. After finishing C
program class, I found that all the code I wrote in C contains little
pointers, obviously I avoid using them.
A few days ago when I was reading a book about programming, I was told
that pointers are the very essence of C language, if I couldn't use it
well, I'm a bad programmer, it's a big shock.
So now I'm wondering: what's the exact position of pointers in C? Is
it really necessary to learn how it works again?
 
S

Stephen Sprunk

Yee.Chuang said:
When I began to learn C, My teacher told me that pointer is the most
difficult part of C, it makes me afraid of it. After finishing C
program class, I found that all the code I wrote in C contains little
pointers, obviously I avoid using them.

That's pretty much what happened with me; I came from a Pascal
background where I had used pointers once or twice in several years, and
before that BASIC, which didn't even have the concept at all.

What's funny is that, when I finally forced myself to learn, pointers
turned out to be a lot easier to deal with than I expected. There just
aren't many good books that teach it right, nor teachers who really
understand it well enough to explain it well to students.
A few days ago when I was reading a book about programming, I was told
that pointers are the very essence of C language, if I couldn't use it
well, I'm a bad programmer, it's a big shock.

I have to agree with that.
So now I'm wondering: what's the exact position of pointers in C? Is
it really necessary to learn how it works again?

You really can't unlock the power that C offers without understanding
and using pointers. If you don't, you're really just writing Pascal
that looks like C.

S
 
C

CBFalconer

Stephen said:
That's pretty much what happened with me; I came from a Pascal
background where I had used pointers once or twice in several
years, and before that BASIC, which didn't even have the concept
at all.

Then you weren't using Pascal thoroughly. The prime uses of
pointers are very similar between Pascal and C, but Pascal doesn't
allow the loose generic conversion of VAR parameters to pointers,
and similar things for arrays, etc. This allows Pascal to check
for most common errors, unlike C.
 
A

August Karlstrom

Yee.Chuang said:
When I began to learn C, My teacher told me that pointer is the most
difficult part of C, it makes me afraid of it. After finishing C
program class, I found that all the code I wrote in C contains little
pointers, obviously I avoid using them.
A few days ago when I was reading a book about programming, I was told
that pointers are the very essence of C language, if I couldn't use it
well, I'm a bad programmer, it's a big shock.
So now I'm wondering: what's the exact position of pointers in C? Is
it really necessary to learn how it works again?

In general you need pointers to build dynamic data structures like
linked lists. In C you also need pointers to simulate call by reference.


August
 
N

Nick Keighley

Then you weren't using Pascal thoroughly.  The prime uses of
pointers are very similar between Pascal and C, but Pascal doesn't
allow the loose generic conversion of VAR parameters to pointers,
and similar things for arrays, etc.  This allows Pascal to check
for most common errors, unlike C.

yes I moved from pascal to C and didn't find pointers a problem.
They seemed very like pascal pointers. Though I thought the sysntax
was *very* strange!

On the other hand I'd programmed in Coral, Assmebler and had
brief exposure to BCPL. So pointers seemed quite normal!


--
Nick Keighley

Unicode is an international standard character set that can be used
to write documents in almost any language you're likely to speak,
learn or encounter in your lifetime, barring alien abduction.
(XML in a Nutshell)
 
Y

Yee.Chuang

yes I moved from pascal to C and didn't find pointers a problem.
They seemed very like pascal pointers. Though I thought the sysntax
was *very* strange!

On the other hand I'd programmed in Coral, Assmebler and had
brief exposure to BCPL. So pointers seemed quite normal!

--
Nick Keighley

Unicode is an international standard character set that can be used
to write documents in almost any language you're likely to speak,
learn or encounter in your lifetime, barring alien abduction.
             (XML in a Nutshell)

Something I forgot to tell: C is the first language I've learned,
after that I understand the basic skills of programming. Most of the
time I just use software like SAS, Matlab and R to solve problems.
Yes, I don't write any software, I just use them.
If learning pointers can help me with programing thoughts or improve
my program skills, I'm glad to do so.
 
V

vippstar

Something I forgot to tell: C is the first language I've learned,
after that I understand the basic skills of programming. Most of the
time I just use software like SAS, Matlab and R to solve problems.
Yes, I don't write any software, I just use them.
If learning pointers can help me with programing thoughts or improve
my program skills, I'm glad to do so.

Pointers are not a unique concept in C.
A pointer points to something. You can access that something via the
pointer.
That's all there is to it, as a generic concept.

Now, if you want to learn C pointers, first you'd have to understand
C's type system.
Given that p is char [4][2], you should immediately be able to tell
which type *p is, p[0][0], &p[0].
(answer: char [2], char, char (*)[2])

That's half the work. The other half is to read the semantics of
pointers.

HTH.
 
R

Richard

Something I forgot to tell: C is the first language I've learned,
after that I understand the basic skills of programming. Most of the
time I just use software like SAS, Matlab and R to solve problems.
Yes, I don't write any software, I just use them.
If learning pointers can help me with programing thoughts or improve
my program skills, I'm glad to do so.

Pointers are not a unique concept in C.
A pointer points to something. You can access that something via the
pointer.
That's all there is to it, as a generic concept.

Now, if you want to learn C pointers, first you'd have to understand
C's type system.
Given that p is char [4][2], you should immediately be able to tell
which type *p is, p[0][0], &p[0].
(answer: char [2], char, char (*)[2])

"p is char[4][2]"? I know pointers and I dont understand your example or
what you are trying to say.
 
B

Bartc

Pointers are not a unique concept in C.
A pointer points to something. You can access that something via the
pointer.
That's all there is to it, as a generic concept.

Not quite. Those other languages use pointers behind the scenes, they
already have the 'something'.

C needs pointers just to implement those 'something's in the first place, or
to push them around. They're not an optional extra, not for dealing with
dynamic or flexible data.
Now, if you want to learn C pointers, first you'd have to understand
C's type system.
Given that p is char [4][2], you should immediately be able to tell
which type *p is, p[0][0], &p[0].
(answer: char [2], char, char (*)[2])

I wouldn't frighten off the OP with this stuff. I don't understand half of
it either.
 
P

Pilcrow

Yee.Chuang said:


He probably told you that not because they're hard to learn (they aren't!),
but because they're hard to teach!
Maybe pointers were mysterious to him. Or he felt them too dangerous
for a beginner, which we hear often. But we learn best by making
mistakes and then correcting them. Taking risks is the best part of
living.
Overcoming your fears is glorious!
 
C

CBFalconer

Yee.Chuang said:
Something I forgot to tell: C is the first language I've learned,
after that I understand the basic skills of programming. Most of the
time I just use software like SAS, Matlab and R to solve problems.
Yes, I don't write any software, I just use them.
If learning pointers can help me with programing thoughts or improve
my program skills, I'm glad to do so.

If C pointers are bothering you, you might consider first learning
Pascal and handling pointers therein. Then returning to C would
mean abandoning the safety and adding new capabilities.
 
R

Richard

CBFalconer said:
If C pointers are bothering you, you might consider first learning
Pascal and handling pointers therein. Then returning to C would
mean abandoning the safety and adding new capabilities.

That is atrocious advice. And would certainly lead to expectations not
met by C.

Anyway outside of c.l.c pointers are easily taught. They are an address
where some data is. You can de-reference that address to get the data
there. You can advance the pointer to point to different addresses.

Trivial stuff when you do not try to be too clever and blind the poor
nOOB with ridiculous nonsense not applicable to their system at too
early a stage.
 
Y

Yee.Chuang

That is atrocious advice. And would certainly lead to expectations not
met by C.

Anyway outside of c.l.c pointers are easily taught. They are an address
where some data is. You can de-reference that address to get the data
there. You can advance the pointer to point to different addresses.

Trivial stuff when you do not try to be too clever and blind the poor
nOOB with ridiculous nonsense not applicable to their system at too
early a stage.

Hey, Richard, it's not so serious about that so called "atrocious
advice". I came here for your advise. Thanks for all of your
instructions, now I know more about points than I used to do, that's
great and fun.
There was no malice in their discussions.
 
R

Richard

Yee.Chuang said:
Hey, Richard, it's not so serious about that so called "atrocious
advice". I came here for your advise. Thanks for all of your
instructions, now I know more about points than I used to do, that's
great and fun.
There was no malice in their discussions.

I realise there was no malice. But it was a silly idea to tell someone
to learn an entire different language to teach what is, after all, a
relatively simple concept when approached in the correct
manner. Especially an effectively dead language such as Pascal which
Falconer seems to love.
 
C

CBFalconer

Richard said:
.... snip ...


That is atrocious advice. And would certainly lead to expectations
not met by C.

Except that it duplicates my experience of long long ago, and I
have no problems with C pointers.
 
R

Richard

CBFalconer said:
Except that it duplicates my experience of long long ago, and I
have no problems with C pointers.

Of long long ago.

I have experience of new C programmers and have never, ever had a
problem explaining them pointers and de referencing pointers. I tend to
use a debugger a block of memory. Easy.

They do not need to learn a dead language like Pascal to understand
pointers.
 
C

Chad

When I began to learn C, My teacher told me that pointer is the most
difficult part of C, it makes me afraid of it. After finishing C
program class, I found that all the code I wrote in C contains little
pointers, obviously I avoid using them.
A few days ago when I was reading a book about programming, I was told
that pointers are the very essence of C language, if I couldn't use it
well, I'm a bad programmer, it's a big shock.
So now I'm wondering: what's the exact position of pointers in C? Is
it really necessary to learn how it works again?


I personally had more issues with the math end of C than pointers
themselves. Yes, I had real issues with the concepts of logical
conjunction, disjunction, implication, and NAND. De Morgans law also
bit me. With that, I'm just going back to being a silent bystander on
this forum.
 
S

sh.vipin

So now I'm wondering: what's the exact position of pointers in C? Is
it really necessary to learn how it works again?

Try to solve the following problem based on your current knowledge.
If you are able to solve it without using the pointers, you don't need
to learn.

/* Puzzle code*/

void X(?????){
???????
}

int main(int cnt, char *aa[]){
int a;
a = 5;
X(??????); //line # 5
printf("\n Value of a is %d",a);
retrun 0;

}

---------------Desired OUTPUT -----------
Value of a is 20

Problem Statement
-----------------------
In the above code, at all the places where you see "?????" you have to
write some C Code.
you have to write the code such that without modifying the variable
"a" in main, value of "a" becomes 20. That is the output of program
when run is as shown in desired output.


Once you finish this, you will realize that there are many cases/
problems which cann;t be solved without using the pointers.
 
K

Keith Thompson

Richard said:
Anyway outside of c.l.c pointers are easily taught. They are an address
where some data is. You can de-reference that address to get the data
there. You can advance the pointer to point to different addresses.

Trivial stuff when you do not try to be too clever and blind the poor
nOOB with ridiculous nonsense not applicable to their system at too
early a stage.

Treating pointers simply as machine addresses can easily lead to the
assumption of a single linear address space. That's a common
implementation, but it's not required. For example, as far as C is
concerned, pointers to two independently declared objects have no
defined relationship to each other (other than inequality), and even
computing ``&x < &y'' invokes undefined behavior.

A C pointer is a more abstract concept than you're implying, defined
in such a way that a machine address pointing somewhere in monolithic
linear memory is one way, but not the only way, to implement them.
 
K

Keith Thompson

So now I'm wondering: what's the exact position of pointers in C? Is
it really necessary to learn how it works again?

Try to solve the following problem based on your current knowledge.
If you are able to solve it without using the pointers, you don't need
to learn.

/* Puzzle code*/

void X(?????){
???????
}

int main(int cnt, char *aa[]){

main's two parameters can legally be given any name you like, but
they're traditionally called argc and argv. Calling them anything
else is obfuscation. And since you don't use them, you can omit them,
declaring main as "int main(void)".
int a;
a = 5;

Ok, but why not use an initializer? "int a = 5;".
X(??????); //line # 5

Um that's not line 5, unless the definition of X is in a separate
source file.
printf("\n Value of a is %d",a);

You have a call to printf. Where's the required #include <stdio.h>?

I don't know where this bizarre habit of putting the "\n" at the
beginning of a line rather than at the end came from. This prints an
unnecessary blank line, and fails to properly terminate the output
line. Some implementations may require a terminating "\n" for valid
output.

printf("Value of a is %d\n", a);
retrun 0;

It's spelled "return". Sure, it's a minor error, but one that you
couldn't have made if you'd bothered to compile your code before
posting it. (Some of my own dumbest mistakes here have been the
result of assuming I could just write code off the top of my head
without bothering to compile it.)
}

---------------Desired OUTPUT -----------
Value of a is 20

Problem Statement
-----------------------
In the above code, at all the places where you see "?????" you have to
write some C Code.
you have to write the code such that without modifying the variable
"a" in main, value of "a" becomes 20. That is the output of program
when run is as shown in desired output.

It's not possible for the value of a to become 20 unless you modify
it. You mean that the code shouldn't *directly* modify a.
Once you finish this, you will realize that there are many cases/
problems which cann;t be solved without using the pointers.

It's probably better just to read about pointers in some good tutorial
or reference work, such as K&R2.
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top