What's the position of pointers

Discussion in 'C Programming' started by Yee.Chuang, Sep 11, 2008.

  1. Yee.Chuang

    Yee.Chuang Guest

    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?
     
    Yee.Chuang, Sep 11, 2008
    #1
    1. Advertising

  2. Yee.Chuang wrote:
    > 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
     
    Stephen Sprunk, Sep 11, 2008
    #2
    1. Advertising

  3. Yee.Chuang

    CBFalconer Guest

    Stephen Sprunk wrote:
    > Yee.Chuang wrote:
    >
    >> 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.


    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.

    --
    [mail]: Chuck F (cbfalconer at maineline dot net)
    [page]: <http://cbfalconer.home.att.net>
    Try the download section.
     
    CBFalconer, Sep 11, 2008
    #3
  4. Yee.Chuang wrote:
    > 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
     
    August Karlstrom, Sep 12, 2008
    #4
  5. On 11 Sep, 22:28, CBFalconer <> wrote:
    > Stephen Sprunk wrote:
    > > Yee.Chuang wrote:


    > >> 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.

    >
    > 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)
     
    Nick Keighley, Sep 12, 2008
    #5
  6. Yee.Chuang

    Yee.Chuang Guest

    On Sep 12, 5:03 pm, Nick Keighley <>
    wrote:
    > On 11 Sep, 22:28, CBFalconer <> wrote:
    >
    >
    >
    > > Stephen Sprunk wrote:
    > > > Yee.Chuang wrote:
    > > >> 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.

    >
    > > 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)


    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.
     
    Yee.Chuang, Sep 12, 2008
    #6
  7. Yee.Chuang

    Guest

    On Sep 12, 12:20 pm, "Yee.Chuang" <> wrote:
    > On Sep 12, 5:03 pm, Nick Keighley <>
    > wrote:
    >
    > > On 11 Sep, 22:28, CBFalconer <> wrote:

    >
    > > 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!

    >
    > 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.
     
    , Sep 12, 2008
    #7
  8. Yee.Chuang

    Richard Guest

    writes:

    > On Sep 12, 12:20 pm, "Yee.Chuang" <> wrote:
    >> On Sep 12, 5:03 pm, Nick Keighley <>
    >> wrote:
    >>
    >> > On 11 Sep, 22:28, CBFalconer <> wrote:

    >>
    >> > 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!

    >>
    >> 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.
     
    Richard, Sep 12, 2008
    #8
  9. Yee.Chuang

    Bartc Guest

    <> wrote in message
    news:...
    > On Sep 12, 12:20 pm, "Yee.Chuang" <> wrote:


    >> 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.


    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.

    --
    Bartc
     
    Bartc, Sep 12, 2008
    #9
  10. Yee.Chuang

    Pilcrow Guest

    On Thu, 11 Sep 2008 12:41:13 +0000, Richard Heathfield
    <> wrote:

    >Yee.Chuang said:
    >
    >> When I began to learn C, My teacher told me that pointer is the most
    >> difficult part of C,

    >
    >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.

    >> it makes me afraid of it.

    Overcoming your fears is glorious!

    >
    >Yes, you're not alone. But it's worth overcoming that fear, because
    >pointers are the very essence of the C language. And once you "get" them,
    >your eyes will light up, and the word "POWER!" will pop into your head,
    >and there'll be no stopping you.
     
    Pilcrow, Sep 12, 2008
    #10
  11. Yee.Chuang

    CBFalconer Guest

    "Yee.Chuang" wrote:
    > Nick Keighley <> wrote:
    >> CBFalconer <> wrote:
    >>> Stephen Sprunk wrote:
    >>>> Yee.Chuang wrote:
    >>>>
    >>>>> 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.
    >>>
    >>> 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!

    >
    > 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.

    --
    [mail]: Chuck F (cbfalconer at maineline dot net)
    [page]: <http://cbfalconer.home.att.net>
    Try the download section.
     
    CBFalconer, Sep 12, 2008
    #11
  12. Yee.Chuang

    Richard Guest

    CBFalconer <> writes:

    > "Yee.Chuang" wrote:
    >> Nick Keighley <> wrote:
    >>> CBFalconer <> wrote:
    >>>> Stephen Sprunk wrote:
    >>>>> Yee.Chuang wrote:
    >>>>>
    >>>>>> 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.
    >>>>
    >>>> 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!

    >>
    >> 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.


    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.
     
    Richard, Sep 12, 2008
    #12
  13. Yee.Chuang

    Yee.Chuang Guest

    On Sep 12, 11:45 pm, Richard<> wrote:
    > CBFalconer <> writes:
    > > "Yee.Chuang" wrote:
    > >> Nick Keighley <> wrote:
    > >>> CBFalconer <> wrote:
    > >>>> Stephen Sprunk wrote:
    > >>>>> Yee.Chuang wrote:

    >
    > >>>>>> 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.

    >
    > >>>> 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!

    >
    > >> 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.

    >
    > 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.
     
    Yee.Chuang, Sep 13, 2008
    #13
  14. Yee.Chuang

    Richard Guest

    "Yee.Chuang" <> writes:

    > On Sep 12, 11:45 pm, Richard<> wrote:
    >> CBFalconer <> writes:
    >> > "Yee.Chuang" wrote:
    >> >> Nick Keighley <> wrote:
    >> >>> CBFalconer <> wrote:
    >> >>>> Stephen Sprunk wrote:
    >> >>>>> Yee.Chuang wrote:

    >>
    >> >>>>>> 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.

    >>
    >> >>>> 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!

    >>
    >> >> 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.

    >>
    >> 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.


    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.
     
    Richard, Sep 13, 2008
    #14
  15. Yee.Chuang

    CBFalconer Guest

    Richard wrote:
    > CBFalconer <> writes:
    >

    .... snip ...
    >
    >> 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.


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

    --
    [mail]: Chuck F (cbfalconer at maineline dot net)
    [page]: <http://cbfalconer.home.att.net>
    Try the download section.
     
    CBFalconer, Sep 13, 2008
    #15
  16. Yee.Chuang

    Richard Guest

    CBFalconer <> writes:

    > Richard wrote:
    >> CBFalconer <> writes:
    >>

    > ... snip ...
    >>
    >>> 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.

    >
    > 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.
     
    Richard, Sep 13, 2008
    #16
  17. Yee.Chuang

    Chad Guest

    On Sep 11, 5:02 am, "Yee.Chuang" <> wrote:
    > 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.
     
    Chad, Sep 13, 2008
    #17
  18. Yee.Chuang

    Guest


    > 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.
     
    , Sep 13, 2008
    #18
  19. Richard<> writes:
    [...]
    > 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.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Sep 13, 2008
    #19
  20. writes:
    >> 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.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Sep 13, 2008
    #20
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Luqman
    Replies:
    1
    Views:
    662
    Luqman
    Feb 7, 2006
  2. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    694
  3. James Wong
    Replies:
    4
    Views:
    515
    James Wong
    Jul 14, 2004
  4. Replies:
    3
    Views:
    187
  5. James Black
    Replies:
    0
    Views:
    407
    James Black
    May 28, 2006
Loading...

Share This Page