Thread: Pointers

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

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

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

  6. #6
    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].
    ?

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

  8. #8
    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 ?

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

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

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

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Why not show code to demonstrate what you mean?
    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.

  13. #13
    Registered User
    Join Date
    Feb 2008
    Posts
    43
    Quote Originally Posted by Elysia View Post
    Why not show code to demonstrate what you mean?
    check my 1st post

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    All I see is this
    Code:
    int main()
    {
    	f1(array);
    	return 0;
    }
    
    void f1(int* x)
    {
    	printf("&#37;d",x);
    }
    Are you wonder why this is working or something else?
    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.

  15. #15
    Registered User
    Join Date
    Feb 2008
    Posts
    43
    the codes in my 1st post are

    Code:
    #include<stdio.h>
    float *var,radius=4;
    
    main()
    {
    *var=&radius;
    printf("var has the address of %d and it points to %f",var,*var);
    return 0;
    }
    Where the *var = &radius is not working

    the 2nd code is

    Code:
    #include<stdio.h>
    int array[10];
    void f1(int);
    
    main()
    {
    	f1(array);
    	return 0;
    }
    
    void f1(int *x)
    {
    printf("%d",x);
    }
    where it seems to work ...

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