convert return value to a pointer

This is a discussion on convert return value to a pointer within the C Programming forums, part of the General Programming Boards category; Hi All I need to fill a float pointer with the float value returned by atof: Code: #include <stdio.h> #include ...

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    72

    convert return value to a pointer

    Hi All

    I need to fill a float pointer with the float value returned by atof:

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    int main(void) {
      float *f = (float *)atof("12.13") ;
    
      printf("input is %f\n", *f) ;
    }
    atof returns a float not a float*. Is there a way to do this without doing

    Code:
    float dum = atof("12.13") ;
    float *f = &dum ;
    I guess I prefer a one-liner!

    thnx
    LuCa

  2. #2
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    Code:
    float *pointer = malloc(sizeof(float)); *pointer = atof("12.13");

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    72
    this is actually not a one-liner

    So, is there no way you can do something like
    Code:
    float *f = &(atoi("12.13")) ;

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Does the float pointer already point to valid memory that you want to change? If so, then you can skip the malloc.

  5. #5
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    Propably not. or use this:
    Code:
    float flt = atof("12.13"); float *pointer = &flt;

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by jeanluca View Post
    this is actually not a one-liner
    Unlike in some other contexts, C and C++ programmer's are not hot about the "all-in-one-liner"; possibly this is because the language does not really lend itself to that concept very well. just to let you know

    Unless you want to write a function to do it:
    Code:
    float *my_atof(char *string) {
          float *ptr = malloc(sizeof(float));
          *ptr = atof(string);
          return ptr;
    }
    Now:
    Code:
    float *x = my_atof("12.13");
    which might be handy if you have to do it enough times, and you really need a pointer that can be passed around *and* modified (if you do not need to change this value later, you do not need a pointer).

    Remember to free() pointers you have malloc'd when you are done with them:
    Code:
    free(ptr);
    ptr=NULL;
    B/T/W: The newer C99 function strtof() would be considered safer and more portable than atof().
    Last edited by MK27; 06-07-2009 at 08:53 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    72
    I've a perl background, I guess thats where the one-liner-wish comes from

    I see I have to drop that with C!

    thnx for pointing this out!

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The return value of a function doesn't (necessarily) have an address. So using the & operator on such a value won't work. For example, in x86, the float may well reside in st(0) - the top of the floating point register stack. This register, obviously, isn't a memory location, so thus doesn't have an address. Similarly if you have an integer return value, it will be in eax. Again, it has no address.

    You obviously can't make a pointer to something that doesn't have an address!

    --
    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
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    I think it's rather unlikely that you need a pointer in the first place. Maybe you might pass a pointer to a function, so that the function can assign a value to the parameter; but then you'd call the function with the address of the variable you want it to fill, like so:
    Code:
    int x;
    func(&x);
    
    void func(int *x) {
        *x = 5;
    }
    I can't see why you'd need to do what you are describing. Can you describe your problem in more detail?

    [edit]
    Unlike in some other contexts, C and C++ programmer's are not hot about the "all-in-one-liner"; possibly this is because the language does not really lend itself to that concept very well.
    Uh huh. How many times have you seen code like this?
    Code:
    void Stack::push(Type data) { stack[top ++] = data; }
    while(*p++ == *q ++);
    for(p = string; *p && isspace(*p); p ++);
    std::cout << "Your answer is " << (correct ? "right" : "wrong") << '.' << std::endl;
    I think it's almost as bad as Perl. If you want a language you really can't do one-liners in, try Python.
    [/edit]
    Last edited by dwks; 06-08-2009 at 01:22 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How can I make this code more elegant?
    By ejohns85 in forum C++ Programming
    Replies: 3
    Last Post: 04-02-2009, 08:55 AM
  2. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  3. Replies: 8
    Last Post: 03-10-2008, 11:57 AM
  4. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  5. OpenGL and Windows
    By sean345 in forum Game Programming
    Replies: 5
    Last Post: 06-24-2002, 10:14 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21