Thread: Problems returning char *

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    12

    Problems returning char *

    Hi!, I'm new and speak spanish...Therefore I hope that you can understand me and help.

    My problem is the next:

    I have a function with this statement

    char * consultaCuenta(char *buffer, char *status)

    Inside I declare a variable:

    char retorno[199];

    and then i do this (after fill retorno with data):

    sprintf(buffer,"%189.189s",retorno)

    return (buffer);

    I receive the return in a variable:

    char *v;

    The compiler says (I translate):

    "the return make (build??) a pointer from an integer without a conversion"

    how I did wrong? what's the problem?

    thanks for the help.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You should probably show code instead of trying to explain. I can't say why the compiler throws that warnings if I don't see the source since if you do as you describe, then you should get no warnings.
    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
    Jan 2008
    Posts
    12
    Sorry...here the code goes :-)

    The source is not pure C is Pro*C (but the pointers are the same :-) )

    Code:
    /* Global variables */
    EXEC SQL BEGIN DECLARE SECTION;
        retorno[199];
    EXEC SQL END DECLARE SECTION;
    
    
    char * consultaCuenta(char *buffer)
    {
          EXEC SQL WHENEVER SQLERROR DO      return (sqlError(buffer));
          EXEC SQL DECLARE CD CURSOR for
            select lpad(num_doc,11,' ')|  lpad(MONTO,11,' ')||lpad(NUM_CONSEJERA,12,' ')||estado
               from AVON_CAJAVEC
               where
               num_doc = lpad(:pecnmro_servicio,10,'0');
    
             EXEC SQL OPEN CD;
             EXEC SQL WHENEVER NOT FOUND DO   break;
    
               for (;;)
                 {
                   EXEC SQL FETCH CD INTO :retorno;
                   sprintf(buffer,"%198.198s",  retorno);
                 }
             EXEC SQL CLOSE CD;
     return (buffer);
    }
    
    the function where the results is received is too long to put here, but the important sentences are this:
    
    char *buff_aux;
    
     buff_aux = consultaCuenta(buffer);
    thanks again

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I don't know Pro-C or what it's called!
    The warning is on the ...
    sprintf(buffer,"%198.198s", retorno);
    line?
    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.

  5. #5
    Registered User
    Join Date
    Jan 2008
    Posts
    12
    Pro*c is the same than C but with SQL sentences...nothing more.

    Yes!!...the return make (build??) a pointer from an integer without a conversion...is in that line the warning.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You mean to say THIS is
    retorno[199];
    retorno, a global INT variable?

    Always specify a return type. ALWAYS.
    Avoid global variables. It can just as well be local here.
    You're trying to pass int to printf, which in this case expects char* because you're using %s.
    So I assume the database has a integer value stored, or something? You're using an array of integers, which makes it even more confusing.
    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
    Registered User
    Join Date
    Jan 2008
    Posts
    12
    I'm sorry...i'm wrong with the declaration.

    The type is char...i forgot to put there...

    char retorno[199]

    that's the declaration...

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    If it's char, then there should be no problem. But from I saw in your source, it lacked a type, thus it was int.
    If it's changed to char, all should work.
    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
    Registered User
    Join Date
    Jan 2008
    Posts
    12
    Thanks Elysia for your time and patience. The mistake must be there in other place. I'll check the entire code...thanks

    Greetings from Chile!!

  10. #10
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    I'm not sure if this is the cause of your problem, but one bad thing I see is that you pass a char* to the function without passing the size of the char* array, then you write to it. If you pass a pointer to a function that is going to be written to, you should always pass the size of the buffer, otherwise you might write too much data.

  11. #11
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    I'm surprised that it gave the error that it did. Normally, in C, variadic functions do not perform conversions from integers to pointers, regardless of the format string. There for the above code should compile to undefined behavior. I guess Pro*c is safer in this way.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  2. lvalue error trying to copy between structures
    By emanresu in forum C Programming
    Replies: 2
    Last Post: 11-16-2006, 06:53 AM
  3. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  4. more pointer problems
    By dharh in forum C Programming
    Replies: 3
    Last Post: 02-11-2003, 06:52 PM
  5. Searching a linked list for char
    By spentdome in forum C Programming
    Replies: 3
    Last Post: 05-22-2002, 11:11 AM