Thread: C Warning message

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    12

    C Warning message

    I have the following c function that returns a random number between 0 and maxR.

    Code:
    #include <stdio.h>
    char LB_Random(char *maxR,char *dummy)
    	{	
    	int i;
    	static char n[10];
    	int rMax = (int)maxR;	
    	rMax++;
    	srand(time(NULL));
    	i = rand() % rMax;
    	strcpy(n,(char*)i);
    	return n;	
    	}
    When I compile it it points the return line giving this warning message:

    warning C4047: 'return' : 'unsigned char ' differs in levels of indirection from 'unsigned char [10]'

    Should I be worried?

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    1. you have very strange castings in your code - show exsample how you plan to call your function
    2. char and char[10] is indeed two different types
    3. srand should be used once per program - not before any call to rand
    4. returning pointer to the local variable (even static) is not a good practice - should be avoided if possible
    5. rand is declared in stdlib as I remember, strcpy - string.h, time() in time.h stdio.h is not used
    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

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Yes you should be worried.

    You return n, an array, which differs in indirection from an unsigned char, the apparent type of the function's return value.

    I'm also uncomfortable with the fact that you could end up calling srand more than once if you use this function more than once. There is also an awful lot of casting magic going on. You need to learn how to convert strings and numbers interchangeably in C.

  4. #4
    Registered User
    Join Date
    Aug 2003
    Posts
    12
    Well the string maxR get's passed in from an external program, but it will be a number. So that's why I need to convert it back to an integer so that I can use it in my calculations. What would be the proper way of doing this?

    The way I understood srand was that i generated a seed for rand, so if the seed is the same every time, you'll get the same random number out, hence why i'm using the system time as the seed, as it shall be different every time it get's called.

    It's been a while since i've done C programming, what would be the correct way of declaring the function. I have to use a static char array as the memory being used by the return parameter is not being automatically released. So I have to return this value.

    If those functions are not in that header file, how does it compile in the first place?

    Perhaps give me an example of how you would re-structure this code?

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    so the function is called like this:
    Code:
    int a = 10;
    LB_Random((char*)&a,NULL)?
    or like this:
    LB_Random("10",NULL);

    The way I understood srand was that i generated a seed for rand, so if the seed is the same every time, you'll get the same random number out, hence why i'm using the system time as the seed, as it shall be different every time it get's called.
    So in general - instead of using random generator - you are using seed generator based on time. Noone said it will be a good distribution. Seed is used once per program - then only rand is called...
    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
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    > The way I understood srand was that i generated a seed for rand.
    This is right, and then the rest of what you type is a result of a misunderstanding. Pseudorandom number generators like the one in rand only need to be seeded once and only once to set off an arithmetic series. This series produces "random" numbers over a period of time, after which the series repeats. There's really not much you can do about the latter part except implement something with a better period if it's not random enough.

    If you want to return a static array, that's fine, just be aware that you are returning a pointer to char, and not a char. The pointer which you return points to the first element in the array.

    > If those functions are not in that header file, how does it compile in the first place?
    Your compiler warnings must not be set high enough. There should be options available to change this somewhere.

    A more correct demonstration to convert numbers and strings was linked to in my previous post.

  7. #7
    Registered User
    Join Date
    Aug 2003
    Posts
    12
    Quote Originally Posted by vart
    so the function is called like this:
    Code:
    int a = 10;
    LB_Random((char*)&a,NULL)?
    or like this:
    LB_Random("10",NULL);


    So in general - instead of using random generator - you are using seed generator based on time. Noone said it will be a good distribution. Seed is used once per program - then only rand is called...
    Called like the second one.

    Sorry i'm not sure what you're saying here. Is the seed not used for rand?

  8. #8
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    if ypur function is called like second one - casting the pointer to int is not enough, you should convert it to int using something like strtol

    is the calling function want to receive the string like "8" - better way IMHO - it should provide a buffer to be filled by your function.
    returning pointers to the static array is not thread safe.

    Is the seed not used for rand?
    It is used...
    seed(time());
    ...
    rand()
    ...
    rand()
    ...
    rand()
    ...
    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

  9. #9
    Registered User
    Join Date
    Aug 2003
    Posts
    12
    Ok slowly but surely . Here's what i've got now

    Code:
    #include <stdlib.h>
    char LB_Random(char *maxR,char *dummy)
    	{	
    	int i;
    	static char n[10];
    	int rMax = atoi(maxR);	
    	rMax++;
    	i = rand() % rMax;
    	sprintf(n,"%d",i)
    	return n;	
    	}
    I'm still conscious on how I define my function in the first place, i.e. i'm return a char where as I want to return a static char array, how do I do this in C?

  10. #10
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    char* LB_Random(char *maxR,char *dummy)
    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

  11. #11
    Registered User
    Join Date
    Aug 2003
    Posts
    12
    So does this now look like better C code? Thanks for all your help guys

    Code:
    #include <stdlib.h>
    char* LB_Random(char *maxR,char *dummy)
    	{	
    	int i;
    	static char n[10];
    	int rMax = atoi(maxR);	
    	rMax++;
    	i = rand() % rMax;
    	sprintf(n,"%d",i);
    	return n;	
    	}
    Last edited by PaulStat; 11-28-2006 at 06:07 AM.

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    What exactly is dummy doing?

    Also as already mentioned, returning a pointer to a static local isn't that good either.

    char *p1 = LB_Random("10",NULL);
    char *p2 = LB_Random("100",NULL);

    p1 AND p2 both point at the same static array. The first result is effectively lost.

    strtol() should be used in preference to atoi(), since it has better error detection.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strange string behavior
    By jcafaro10 in forum C Programming
    Replies: 2
    Last Post: 04-07-2009, 07:38 PM
  2. File Server Help
    By lautarox in forum C Programming
    Replies: 146
    Last Post: 09-24-2008, 06:32 PM
  3. Need some help...
    By darkconvoy in forum C Programming
    Replies: 32
    Last Post: 04-29-2008, 03:33 PM
  4. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 03:25 AM
  5. warning message
    By potbelle in forum C Programming
    Replies: 11
    Last Post: 04-03-2002, 10:20 PM