Thread: new c programmer have problem with pointer....

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    2

    new c programmer have problem with pointer....

    http://www.exforsys.com/tutorials/c-...-pointers.html

    the tutorial above is referred.

    i follow the instruction in the tutorial, unfortunately, when i try to compile it, some error was occurred...

    Code:
    main()
    {
    int *ptr;
    int sum;
    sum=45;
    ptr=?
    printf (“\n Sum is %d\n”, sum);
    printf (“\n The sum pointer is %d”, ptr);
    }
    and this is the error...

    Code:
    Compiler: Default compiler
    Executing  g++.exe...
    g++.exe "D:\C\pointer\04.cpp" -o "D:\C\pointer\04.exe"    -I"d:\Dev-Cpp\lib\gcc\mingw32\3.4.2\include"  -I"d:\Dev-Cpp\include\c++\3.4.2\backward"  -I"d:\Dev-Cpp\include\c++\3.4.2\mingw32"  -I"d:\Dev-Cpp\include\c++\3.4.2"  -I"d:\Dev-Cpp\include"   -L"d:\Dev-Cpp\lib" 
    Executing  g++.exe...
    g++.exe "D:\C\pointer\04.cpp" -o "D:\C\pointer\04.exe"    -I"d:\Dev-Cpp\lib\gcc\mingw32\3.4.2\include"  -I"d:\Dev-Cpp\include\c++\3.4.2\backward"  -I"d:\Dev-Cpp\include\c++\3.4.2\mingw32"  -I"d:\Dev-Cpp\include\c++\3.4.2"  -I"d:\Dev-Cpp\include"   -L"d:\Dev-Cpp\lib" 
    D:\C\pointer\04.cpp: In function `int main()':
    
    D:\C\pointer\04.cpp:6: error: expected primary-expression before '?' token
    D:\C\pointer\04.cpp:7: error: stray '\147' in program
    D:\C\pointer\04.cpp:7: error: stray '\' in program
    D:\C\pointer\04.cpp:7: error: `n' undeclared (first use this function)
    D:\C\pointer\04.cpp:7: error: (Each undeclared identifier is reported only once for each function it appears in.)
    D:\C\pointer\04.cpp:7: error: stray '\' in program
    D:\C\pointer\04.cpp:7: error: stray '\148' in program
    D:\C\pointer\04.cpp:7: error: `printf' undeclared (first use this function)
    D:\C\pointer\04.cpp:7: error: expected `:' before ';' token
    D:\C\pointer\04.cpp:7: error: expected primary-expression before ';' token
    D:\C\pointer\04.cpp:8: error: stray '\147' in program
    D:\C\pointer\04.cpp:8: error: stray '\' in program
    D:\C\pointer\04.cpp:8: error: stray '\148' in program
    
    Execution terminated
    mmm...what was happened..i followed exactly the tutorial, but still have some error...

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    ptr should be set to point to sum
    ptr=? has no meaning
    I think tutorial wants you to figure out what should be put in place of ?

    %d is for integers %p for pointers
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    2
    oic, thank you for ur prompt reply, really appreciate it
    so i just modified it...and this time there is no error...but i'm not sure whether it is correct or not....

    Code:
    #include<stdio.h>
    
    main()
    {
      int *ptr;
      int sum;
      sum=45;
      ptr=&sum;
      printf ("Sum is %d\n", sum);
      printf ("The sum pointer is %d\n",ptr);
    }
    output

    Code:
    Sum is 45
    The sum pointer is 2293616

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    main should be declared as int
    int main()

    and correct way to print the pointer is using &#37;p format
    the rest seems to be ok
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Code:
    #include<stdio.h>
    
    int main()
    {
      int *ptr;
      int sum;
      sum=45;
      ptr=&sum;
      printf ("Sum is %d\n", sum);
      printf ("The sum pointer is %p\n",ptr);
      printf ("The sum is %d\n",*ptr);
      return 0;
    }

  6. #6
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Here is better tutorial than that Pointers. I see some bugs on your tutorial.

    ssharish

  7. #7
    Registered User
    Join Date
    Sep 2006
    Location
    Beaverton, Oregon, United States
    Posts
    176
    Quote Originally Posted by ssharish2005 View Post
    Here is better tutorial than that Pointers. I see some bugs on your tutorial.

    ssharish
    I am also learning C, and I can tell you from experience it is damn hard to find a decent tuturial that doesn't assume to much, explain too little, or have outright errors. Same problem with VB tutorials I've used.

    It seems that you have to combine like 5 different tutorials just to get a decent grasp of what's going on overall.

    I'm told that Programming in C by Stephen Koch is suppose to be an incredible, all around easy to understand book on the subject. However, I have not yet bought a copy so I don't know whether it is or isn't for myself.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by A34Chris View Post
    I am also learning C, and I can tell you from experience it is damn hard to find a decent tuturial that doesn't assume to much, explain too little, or have outright errors. Same problem with VB tutorials I've used.
    That's probably because it's darn hard to WRITE the tutorials in such a way that you explain the right amount. If you have too much explanation, people will go elsewhere to find the solution. If you don't explain enough, the same problem - but slightly less amount of work.

    The other problem is of course that if you write tutorials, you'd better know the subject quite well, which may also mean that you take things for granted.

    Outright errors should be reported to the author / webmaster of the site posting such code.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    I am also learning C, and I can tell you from experience it is damn hard to find a decent tuturial that doesn't assume to much, explain too little, or have outright errors. Same problem with VB tutorials I've used.
    The reason I gave him a better link is because the old link which he was refereeing wasn't the start link for pointer. It confuses any beginner es. Even some of the example code dosn;t work in that page. Which is like no point looking into that site anymore.

    Pointer are not those topic that would be understandable at a glance, you should got thought it twice or more to clearly understand whats going on behind the senses.

    ssharish

  10. #10
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    And the best way to solve the tutorial problem is...?
    Get a good book, from a good publisher, K&R for example.

    As matsp said, the hard part about writing tutorials is you have to know your stuff damn well but you equally have to know how to explain what you know (on a begginers level) AND why it is so, also how to build-up rather than jumping straight into the deep-end.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointer to pointer realloc problem
    By prakash0104 in forum C Programming
    Replies: 14
    Last Post: 04-06-2009, 08:53 PM
  2. A problem with pointer initialization
    By zyklon in forum C Programming
    Replies: 5
    Last Post: 01-17-2009, 12:42 PM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. pointer problem
    By DMaxJ in forum C Programming
    Replies: 4
    Last Post: 06-11-2003, 12:14 PM