Thread: Problems with Pointers in functions.

Threaded View

Previous Post Previous Post   Next Post Next Post
  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 06:16 PM. Reason: Needed to comment on code more.

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, 01: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, 10: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