Sign in to follow this  
jdub

Problem with C++ code

Recommended Posts

jdub    459
I am pretty new to using pointers. I tried to type up this simple function that would take an empty array and turn it into a deck of random cards: #include "stdafx.h" #include <iostream> #include <stdlib.h> #include <time.h> using namespace std; void newdeck(char *argrank[], char *argsuite[], int suitesize, int ranksize); int main(int argc, _TCHAR* argv[]) { srand(time(NULL)); } (void newdeck(char *argrank[], char *argsuite[], int suitesize, int ranksize) { char *suiteptr; suiteptr = argsuite; char *rankptr; rankptr = argrank; char *suite[] = {"suite1, suite2"}; char *rank[] = {"rank1" , "rank2"}; for (int suitex = 0; suitex < suitesize; suitex++){ *suiteptr = rand() % suitesize; suiteptr++; } for (int rankx = 0; rankx < ranksize; rankx++){ *rankptr = rand() % ranksize; rankptr++; } } vc++ express error code: 1>c:\documents and settings\owner\desktop\c++\cards\cards1.3workingversion\cards1.3workingversion\cards1.3workingversion.cpp(14) : warning C4244: 'argument' : conversion from 'time_t' to 'unsigned int', possible loss of data 1>c:\documents and settings\owner\desktop\c++\cards\cards1.3workingversion\cards1.3workingversion\cards1.3workingversion.cpp(22) : error C2440: '=' : cannot convert from 'char **__w64 []' to 'int *' 1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast 1>c:\documents and settings\owner\desktop\c++\cards\cards1.3workingversion\cards1.3workingversion\cards1.3workingversion.cpp(24) : error C2440: '=' : cannot convert from 'char **__w64 []' to 'int *' 1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast All help is appreciated.

Share this post


Link to post
Share on other sites
jyk    2094
Hi,

Step one is to re-post your code (or edit your original post) using [source] tags. Then, once the code is readable, we can start helping you with it (well, you'll probably get help either way, but it'll be a lot easier for us if the code is formatted nicely).

Share this post


Link to post
Share on other sites
darrenc182    393
The error C2440: '=' : cannot convert from 'char **__w64 []' to 'int *' I believe means that the rand() function does not return characters so you will need to convert the integer value passed from rand() to a character. I also believe that if you use data structures for this particular task would be easier and more structurally sound. Also don't forget to allocate your pointers using the new operator.

Share this post


Link to post
Share on other sites
Futurulus    124
Actually, I think the "char ** _w64[]" errors come from these lines:
char *suiteptr;
suiteptr = argsuite;
char *rankptr;
rankptr = argrank;
You declared argsuite and argrank to be arrays of character strings (char *argrank[]) but you declared suiteptr and rankptr to be single strings (char *rankptr). The compiler can't assign an array of pointers to a single pointer.

If you want suiteptr to point to the first element of the array, use this:
char *suiteptr;
suiteptr = argsuite[0];
Otherwise, make sure either all or none of your types are arrays.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this