Thread: Help with C pointers

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    145

    Help with C pointers

    I have this function:
    Code:
    void func(unsigned *t)
    {
    //do something with t
    }
    If I do the following, I get no compilation warnings, but I get a seg fault
    Code:
    unsigned *temp;
    func(temp);
    If I do this, I get one seg fault(incompatible pointer type), but it behaves how I want it to.
    Code:
    unsigned *temp;
    func(&temp);
    How can I get it working but without the warning?

    thx

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    void func(unsigned int* t)
    {
    //do something with t
    }
    
    unsigned int temp;
    func(&temp);
    Note that type* is just a pointer. It points to some memory location where the value is stored. But since you don't initialize the variable with valid allocated memory, you get a seg fault, a crash.
    Note that a pointer is created by taking the address of a variable or by allocating dynamic memory (ie malloc). If you take the address of a pointer, you get a pointer-to-pointer.
    So...

    Code:
    int x;
    int* y = &x;
    int** z = &y;
    And you actually get an error, not a warning because the standard doesn't allow converting from type** to type* (and it won't work to boot!).
    Keep that in mind.
    Last edited by Elysia; 01-24-2008 at 02:54 PM.
    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.

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    145
    I tried the above but I'm still getting the same warning
    Last edited by Wiretron; 01-24-2008 at 02:58 PM.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Wiretron View Post
    I tried the above but I'm still getting the same warning
    Then you didn't try the above. Post what you have and what error/warning you're getting.

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    145
    passing argument 1 of func from incompatible pointer type

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code, please.
    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.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You cannot get that error from the code above:
    Code:
    void func(unsigned int* t)
    {
    //do something with t
    }
    
    unsigned int temp;
    func(&temp);
    What code do you have, and why isn't it what's above?

  8. #8
    Registered User
    Join Date
    Nov 2005
    Posts
    145
    nvm, all sorted!

    thx
    Last edited by Wiretron; 01-24-2008 at 03:10 PM.

  9. #9
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,337
    try a

    #include <stdio.h>

    at the top of your program.

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    printf is declared in stdio.h. So you need to include that header.

  11. #11
    Registered User vijoeyz's Avatar
    Join Date
    Jan 2008
    Posts
    3
    Quote Originally Posted by Wiretron View Post
    I have this function:
    Code:
    void func(unsigned *t)
    {
    //do something with t
    }
    If I do the following, I get no compilation warnings, but I get a seg fault
    Code:
    unsigned *temp;
    func(temp);
    If I do this, I get one seg fault(incompatible pointer type), but it behaves how I want it to.
    Code:
    unsigned *temp;
    func(&temp);
    How can I get it working but without the warning?

    thx
    Try this:

    Code:
    unsigned *temp;  /* equivalent to unsigned int *temp */
    unsigned ui;
    
    function(&ui);  /* Call funciton() like this */
    --
    Vijay Zanvar
    http://faq.zanvar.in

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by vijoeyz View Post
    Try this:

    Code:
    unsigned *temp;  /* equivalent to unsigned int *temp */
    unsigned ui;
    
    function(&ui);  /* Call funciton() like this */
    --
    Vijay Zanvar
    http://faq.zanvar.in
    The temp variable is unreferenced so you don't need it.
    It's always best to explicitly specify a type. So not "unsigned temp", but "unsigned int temp".
    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.

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