trouble with strndup function

This is a discussion on trouble with strndup function within the C Programming forums, part of the General Programming Boards category; I keep getting the following warning with this code: warning: assignment makes pointer from integer without a cast Referring to ...

  1. #1
    zsaniK Kinasz's Avatar
    Join Date
    Jan 2003
    Posts
    222

    trouble with strndup function

    I keep getting the following warning with this code:

    warning: assignment makes pointer from integer without a cast
    Referring to the line containing the strndup function call. The strndup function is copying the string but it is copying the whole string.

    I am not sure what I am doing wrong, help would be great.



    Code:
      	char *inet;
      	char *str;
      	char  *field_delimeter;
      	char received[BUFSIZ] = "blah blah ( copy me ) blah";
      
      	// Get delimeters
      	str = strchr( received, '(' );
      	field_delimeter =   strrchr( received, ')' );
       
      	// Get new string
      	if (( inet = strndup( str, field_delimeter - str))== NULL){ // this line returns warning
      		printf( "error duping string\n" );
      		return EXIT_FAILURE;
      	}
    "Assumptions are the mother of all **** ups!"

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Did you provide the correct prototype for the strndup function? Did you #include the right file?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    zsaniK Kinasz's Avatar
    Join Date
    Jan 2003
    Posts
    222
    yes, I did (string.h), sorry for not including in my code.

    I just realised it is copying the correct nr of chars I was printing the wrong variable in my test code... that was an hour well wasted :P

    But I am still getting that warning? Dont know why??
    "Assumptions are the mother of all **** ups!"

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Normally the error means the prototype hasn't been provided, so the compiler assumes strndup returns an int, therefore you get an error when converting said int to a pointer.

    strndup is none standard. Are you sure its in string.h (you'll have to open it up and check for yourself).
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    zsaniK Kinasz's Avatar
    Join Date
    Jan 2003
    Posts
    222
    from sting.h
    Code:
    /* Return a malloc'd copy of at most N bytes of STRING.  The
       resultant string is terminated even if no null terminator
       appears before STRING[N].  */
    #if defined __USE_GNU
    extern char *strndup (__const char *__string, size_t __n)
         __THROW __attribute_malloc__;
    #endif
    Is there anything else that could cause the warning?
    "Assumptions are the mother of all **** ups!"

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,417
    Yes, strndup isn't an ANSI function, so if you specified -ansi on the command line, all the GNU extensions (of which this is one) are no longer visible to you due to the conditional compilation.

    Either stop using -ansi (because your program is no longer ANSI-compatible), or write your own implementation of strndup() in terms of ANSI functions.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    zsaniK Kinasz's Avatar
    Join Date
    Jan 2003
    Posts
    222
    I am not using -ansi but I will do my own implementation to solve the problem and go cross platform.

    Thanks Salem
    "Assumptions are the mother of all **** ups!"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  3. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  4. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 01:28 PM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM

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