Problems with Pointers in functions.

This is a discussion on Problems with Pointers in functions. within the C++ Programming forums, part of the General Programming Boards category; I've been working on a way to create a random generator for ints and chars, but eventually I gave up ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Location
    In my house
    Posts
    32

    Problems with Pointers in functions.

    I've been working on a way to create a random generator for ints and chars, but eventually I gave up and started working on a way to make what was already available (rand() from cstdlib).

    I made the functions and put them in a class. I then wanted to make a function which would randomly generate a string, but because I can't return multiple chars, I decided to "point" to a string which was already declared. So far I've been having some problems with calling the function. Every time I do, I get an error saying "Argument of type "Char (*) [255]" is incompatible with parameter of type "char **"". To be honest, I haven't done too much string and pointer manipulation so i'm a bit stumped.

    Heres the entire code: (sorry its a bit hard to read)

    MainHeader.h
    Code:
    #include <cstdlib>
    #include <sys/timeb.h>
    
    class Rand
    {
    private:
    	//system time (__SYSTM) timeb variable for the srand() seed.
    	struct timeb __SYSTM;
    
    public:
    
    	// Rand.Int(Minimum number, Maximum number)
    	// returns (0 <= __MIN <= NUMBER <= __MAX <= RAND_MAX)
    	// example call: Rand.Int(500,1500);
    
    	int Int(int __MIN, int __MAX)
    	{
    		if (__MIN > __MAX || __MIN < 0 || __MAX > RAND_MAX)
    			return -1;
    
    		//gets the system time, and seeds it for rand()
    		ftime(&__SYSTM);
    
    		srand(__SYSTM.millitm);
    
    		//returns a number between the min num and the max num starting from the min num
    		//eg (100, 150) would return the random number between 100 and 150
    		return ((rand() % (__MAX - __MIN) ) + __MIN);
    	}
    
    	//Rand.Char(Capitals)
    	//returns (CHAR) or (char) or random depending on caps variable
    	//example call: Rand.Char(0);
    
    	char Char(int __CAPS)
    	{
    		//gets the system time , and seeds it for rand()
    		ftime(&__SYSTM);
    		srand(__SYSTM.millitm);
    
    		//String Buffer is for the randomization of chars, containing lower case at 0-25 and UPPERCASE at 26-52.
    		char __SBUF[53] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    
    		//Caps > 0 means CAPITALS
    		if (__CAPS > 0)
    			return __SBUF[(rand() % 26) + 26];
    		//Caps < 0 means lowercase
    		else if (__CAPS < 0)
    			return __SBUF[rand() % 26];
    		//Caps == 0 means RaNdOm CaPs.
    		else
    			return __SBUF[rand() % 52];
    	}
    
    	//Rand.String(Storage Buffer, Chars to be generated, Size of Buffer, Capitals)
    	//returns (STRING),  (string) or random depending on caps variable and length
    	//example call: Rand.String(&Buffer, 10, sizeof Buffer, 0);
    
    	int String(char *__BUFR[], int __CHARS, int __SIZOF, int __CAPS)
    	{
    		//count for the loop, and String Buffer for the Characters.
    		int __COUNT = 0;
    		char __SBUF[53] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    
    		if (__SIZOF <= __CHARS || *__BUFR == NULL || __CHARS < 0 || __SIZOF < 0)
    			return -1;
    
    		//from 0 to CHARS, put a random character into *Buffer
    		for(__COUNT; __COUNT <= __CHARS; __COUNT++)
    		{
    			//get system time, and seed rand()
    			ftime(&__SYSTM);
    			srand(__SYSTM.millitm);
    
    			//Caps > 0 means UPPERCASE
    			if(__CAPS > 0)
    				*__BUFR[__COUNT] = __SBUF[(rand() % 26) + 26];
    			//Caps < 0 means lowercase
    			else if (__CAPS < 0)
    				*__BUFR[__COUNT] = __SBUF[(rand() % 26) + 26];
    			//Caps == 0 means RaNdOm CaPs.
    			else
    				*__BUFR[__COUNT] = __SBUF[rand() % 52];
    		}
    
    		//NULL termination
    		*__BUFR[__COUNT] = '\0';
    
    		return 0;
    	}
    
    } Rand;
    Main.cpp
    Code:
    #include <iostream>
    #include "MainHeader.h"
    
    using namespace std;
    
    char Buffer[255];
    
    int main()
    {
    	Rand.String(&Buffer,10, sizeof Buffer, 0);
    	cout << Buffer << endl;
    	return 0;
    }
    Last edited by patrink; 01-20-2011 at 05:16 PM. Reason: Needed to comment on code more.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    char(*)[255] is a pointer to an array of char -- i.e., your &Buffer.

    char** is a pointer to a pointer to char.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    First of all lose all the underscores in front of your function names... They are reserved for name decoration during compilation. Simply choose descriptive names like RandomNumber, InputString etc.

    Second stop naming functions after variable types. That one will come back and bite you big time if you ever forget to capitalize something. Once again, choose descriptive names GetRndInt, GetRndChar, GetRndString, etc.

    Then, lose the all uppercase names. By convention they are reserved for preprocessor sysmbols and typedefs.

    Next you should not initialize srand() more than once and certainly never in a loop. I'm thinking you should do it in your class's constructor.

    What is __BUFR and where is it initialized?

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    In my house
    Posts
    32
    Quote Originally Posted by CommonTater View Post
    First of all lose all the underscores in front of your function names... They are reserved for name decoration during compilation. Simply choose descriptive names like RandomNumber, InputString etc.

    Second stop naming functions after variable types. That one will come back and bite you big time if you ever forget to capitalize something. Once again, choose descriptive names GetRndInt, GetRndChar, GetRndString, etc.

    Then, lose the all uppercase names. By convention they are reserved for preprocessor sysmbols and typedefs.

    Next you should not initialize srand() more than once and certainly never in a loop. I'm thinking you should do it in your class's constructor.

    What is __BUFR and where is it initialized?
    Yeah...sorry about that, I like to complicate things more than they should be.

    __BUFR is initialized in
    Code:
    int String(char *__BUFR[], int __CHARS, int __SIZOF, int __CAPS)
    It (should) point to an array of chars when Rand.String() is called, and within the code, change it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Conversion of pointers to functions
    By hzmonte in forum C Programming
    Replies: 0
    Last Post: 01-20-2009, 12:56 AM
  2. Passing Structure Pointers to Functions
    By samus250 in forum C Programming
    Replies: 15
    Last Post: 03-20-2008, 03:13 PM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  4. API "Clean Up" Functions & delete Pointers :: Winsock
    By kuphryn in forum Windows Programming
    Replies: 2
    Last Post: 05-10-2002, 06:53 PM
  5. Pointers, arrays , functions
    By sballew in forum C Programming
    Replies: 19
    Last Post: 09-16-2001, 11:12 PM

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