Thread: Pointers

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    43

    Pointers

    its been around 1 week i started C.im learning it from "teach yourself C in 21 days".im in the 9th day and ive finished the chapter and doing the exercises.theres an exercise that asks "how to assign the address of a float value called radius to a pointer"
    at the back of the book the answer of this exercise is just

    Code:
    float *var = &radius;
    what i tried on the VC++ 6 compilter is

    Code:
    #include<stdio.h>
    float *var,radius=4;
    
    main()
    {
    *var=&radius;
    printf("var has the address of &#37;d and it points to %f",var,*var);
    return 0;
    }
    unfortunately im getting the error " 1.c(6) : error C2115: '=' : incompatible types "
    I tried var=&radius instead of *var=&radius and it worked.

    but for the case of passing arrays as arguments to functions:

    Code:
    #include<stdio.h>
    int array[10];
    void f1(int);
    
    main()
    {
    	f1(array);
    	return 0;
    }
    
    void f1(int *x)
    {printf("%d",x);}
    After compiling im getting warnings.
    1.c(7) : warning C4047: 'function' : 'int ' differs in levels of indir
    m 'int [10]'
    1.c(7) : warning C4024: 'f1' : different types for formal and actual p
    1.c(12) : warning C4028: formal parameter 1 different from declaration

    but the * in the void f1(int) is fixing the warnings

    can someone explain why doesn't the *var=&radius work in the first code but when passing array to the *x in function f1 (as void f1(int *x)) its working ? like they are the same thing ... in the second code we are trying to *x = array , by passing array to the argument ( int *x)
    also why is the Warnings for and how come they are fixed when in the function prototype the f1(int) is replaced by f1(int *) ?
    Last edited by BlaX; 02-21-2008 at 11:48 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I tried var=&radius instead of var=&radius and it worked.
    var is a pointer, *var is what it points to.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    43
    Quote Originally Posted by laserlight View Post
    var is a pointer, *var is what it points to.
    yeah i know, i knew that its done by
    var = &radius.

    but the answer in the book confused me.
    plus it is working when trying to pass an address as an argument when the function's argument is *x. its like *x = &array[0].
    ?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by BlaX View Post
    yeah i know, i knew that its done by
    var = &radius.

    but the answer in the book confused me.
    plus it is working when trying to pass an address as an argument when the function's argument is *x. its like *x = &array[0].
    ?
    I think you're getting confused between initializing and assignment. The variable declaration
    Code:
    float *var = &radius;
    is the same as the code
    Code:
    float *var;
    var = &radius;
    The variable is var, not *var; the type is float * (i.e., pointer-to-float), not float.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by tabstop View Post
    The variable is var, not *var; the type is float * (i.e., pointer-to-float), not float.
    Which is why I always do type* var instead of type *var!
    Makes it easier to distinguish type from variable. Type is type* and variable name is var!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Feb 2008
    Posts
    43
    how should i know all these if they arent mentioned in the book !... this book sucks

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by Elysia View Post
    Which is why I always do type* var instead of type *var!
    Makes it easier to distinguish type from variable. Type is type* and variable name is var!
    Which is why I always do type *var instead of type* var!
    Makes it easier to distinguish type of the dereferenced variable: *var is a type!
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    First of all, main should return int.
    Second, I can suggest you actually update your compiler. VC6 is very old and outdated.
    And as for your problems, function prototypes and definitions must match each other. The compiler will look at the declaration (prototype) of the function and sees "int", yet you try to pass int*, so it complains.
    And I suggest perhaps you re-read the pointers part a little, because doing *var will actually dereference the pointer and not assign something to the pointer itself, but to the memory it points to. Thus you try to assign int* to int, which also won't work.

    Code:
    {printf("&#37;d",x);}
    I wouldn't recommend doing that either. Firstly, separate the lines:
    Code:
    {
    	printf("%d",x);
    }
    Second, don't print pointers using %d. There's a special type for that. Don't remember what it is, though.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by Elysia View Post
    Second, don't print pointers using %d. There's a special type for that. Don't remember what it is, though.
    it is %p - easy for pointer
    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

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Second, don't print pointers using &#37;d. There's a special type for that. Don't remember what it is, though.
    cppreference.com says it is %p, but I cannot be bothered to double check with C99.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Well, the book is wrong.
    I still don't fully understand your first question, though?
    And remember that all pointers must have a type. *x is not a pointer, it is dereferencing the pointer named x.
    A real pointer is type* x (or type * x or type *x, whichever you want).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Registered User
    Join Date
    Feb 2008
    Posts
    43
    Quote:
    Originally Posted by Elysia View Post
    First of all, main should return int.
    Second, don't print pointers using %d. There's a special type for that. Don't remember what it is, though.
    im just in the 9th chapter.havent finished the whole book yet.all i know are the %d,%f,%c and some which i still didnt use.
    and throughout the chapter all the pointers were printed using %d.

    for the return, its still =0 throughout the 9 chapters

    Quote:
    Originally Posted by Elysia View Post
    First of all, main should return int.
    Second, I can suggest you actually update your compiler. VC6 is very old and outdated.
    .
    what do u advise me to dl ?

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Here's a list of free compilers:
    http://cpwiki.sf.net/Integrated_Development_Environment

    If you're a student and live in any of the following countrries:
    United States, the United Kingdom, Canada, China, Germany, France, Finland, Spain, Sweden, Switzerland and Belgium
    You can also get the Professional version of Visual Studio free at https://downloads.channel8.msdn.com/
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Registered User
    Join Date
    Feb 2008
    Posts
    43
    thank you pal.im admiring your superfast replies

  15. #15
    Registered User
    Join Date
    Feb 2008
    Posts
    43
    but still the concept of how the *x = &array[0] is true while passing array as an argument to the void f1(int *x) is not clear .

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Variable pointers and function pointers
    By Luciferek in forum C++ Programming
    Replies: 11
    Last Post: 08-02-2008, 02:04 AM
  2. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 05:59 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM