About variable type switching..

This is a discussion on About variable type switching.. within the C Programming forums, part of the General Programming Boards category; I am stuck on a certain point in making a program. That would be using pointers to pull diffrent strings ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    8

    Question About variable type switching..

    I am stuck on a certain point in making a program. That would be using pointers to pull diffrent strings from an array, while still being interchangable with a "char" type.

    For example..

    Code:
    void feats()
    {
    	int x;
    
    	char feat1[] = "Alertness";
    	char feat2[] = "Ambidexterity";
    	char feat3[] = "Armor Proficency(small)";
    	char feat4[] = "Armor Proficency(medium)";
    	char feat5[] = "Armor Proficency(large)";
    
    	void *pf1 = feat1;
    	void *pf2 = feat2;
    	void *pf3 = feat3;
    	void *pf4 = feat4;
    	void *pf5 = feat5;
    
    	srand(time(NULL));
    
    	x = rand()%5;
    
    	if(x == 1)
    	{
    		Feat1 = pf1;
    	}
    	else if(x == 2)
    	{
    		Feat1 = pf2;
    	}
    	else if(x == 3)
    	{
    		Feat1 = pf3;
    	}
    	else if(x == 4)
    	{
    		Feat1 = pf4;
    	}
    	else
    	{
    		Feat1 = pf5;
    	}
    
    	return;
    }
    I have absolutely no idea how to make this accept the pointer's value. Any help would be greatly appriacated..
    Last edited by Dragonlord; 08-31-2002 at 02:49 PM.

  2. #2
    Earth_King
    Guest

    Re: About variable type switching..

    Originally posted by Dragonlord
    I am stuck on a certain point in making a program. That would be using pointers to pull diffrent strings from an array, while still being interchangable with a "char" type.

    For example..



    void feats()
    {
    int x;

    char feat1[] = "Alertness";
    char feat2[] = "Ambidexterity";
    char feat3[] = "Armor Proficency(small)";
    char feat4[] = "Armor Proficency(medium)";
    char feat5[] = "Armor Proficency(large)";

    void *pf1 = feat1;
    void *pf2 = feat2;
    void *pf3 = feat3;
    void *pf4 = feat4;
    void *pf5 = feat5;

    srand(time(NULL));

    x = rand()%5;

    if(x == 1)
    {
    Feat1 = pf1;
    }
    else if(x == 2)
    {
    Feat2 = pf2;
    }
    else if(x == 3)
    {
    Feat3 = pf3;
    }
    else if(x == 4)
    {
    Feat4 = pf4;
    }
    else
    {
    Feat5 = pf5;
    }

    return;
    }



    I have absolutely no idea how to make this accept the pointer's value. Any help would be greatly appriacated..
    you need to typecast when you assign a array name to a void pointer. since data type for array is char so you need to do like this: Feat1 = (char *)pf1;
    do the same for Feat2..Feat3..

    Earth_King

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    8

    Re: Re: About variable type switching..

    Originally posted by Earth_King
    you need to typecast when you assign a array name to a void pointer. since data type for array is char so you need to do like this: Feat1 = (char *)pf1;
    do the same for Feat2..Feat3..

    Earth_King
    Alright. I've tried to do that, but I believe the problem doesn't lie with char[] -> void, I believe the problem is in this: char[] -> void -> char.



    error C2440: '=' : cannot convert from 'char *' to 'char'

    or

    error C2440: '=' : cannot convert from 'void *' to 'char'



    The question I had was on how to get these to accept each other. I can't cout the pointers (nasty), nor can I typecast them to print out anything but strange characters and then have the program die.

    I am thinking that I have to use the array variables themselves in the cout, but even then I can't randomize the variables..

    [edit: Code above..]

  4. #4
    Earth_King
    Guest
    Opse! I did not see that Feat1 or Feat2 are array constants.
    Since Feat1 is name of char array and Feat1 is constant; so
    you are not able to modify after it initialize.
    It not allow to modifyh Feat1 = (char) p;
    Feat1 is always allow on the righthand assignment (RH value).

    Earth_King

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    8
    Originally posted by Earth_King
    Opse! I did not see that Feat1 or Feat2 are array constants.
    Since Feat1 is name of char array and Feat1 is constant; so
    you are not able to modify after it initialize.
    It not allow to modifyh Feat1 = (char) p;
    Feat1 is always allow on the righthand assignment (RH value).

    Earth_King
    That would hold true if the array constants were also the names of the variables I was trying to pass to, but here:

    Code:
    #include <iostream.h>
    #include <cstdlib>
    #include <time.h>
    
    void acgen();
    void hpgen();
    void CHRgen();
    void CONgen();
    void DEXgen();
    void INTgen();
    void STRgen();
    void WISgen();
    void feats();
    void traits();
    void features();
    void spells();
    void special();
    
    int AC;
    int HP;
    int CHR;
    int CON;
    int DEX;
    int INT;
    int STR;
    int WIS;
    char Name[50];
    char Feat1;
    char Feat2;
    char Feat3;
    char Feat4;
    char Feat5;
    char Trait1;
    char Trait2;
    char Trait3;
    char Trait4;
    char Trait5;
    char Features;
    char Spells;
    char Special;
    In other words, what I am trying to pass to are just more char type variables, not the arrays themselves.

    Also, in main() you'll see what I mean...

    Code:
    void main()
    {
    	char x;
    
    	cout << "What is the Monster Name? ";
    	cin >> Name;
    	cout << endl;
    
    	acgen();
    	cout << "AC: " << AC << endl;
    
    	hpgen();
    	cout << "HP: " << HP << endl;
    	
    	CHRgen();
    	cout << "CHR: " << CHR << endl;
    	
    	CONgen();
    	cout << "CON: " << CON << endl;
    	
    	DEXgen();
    	cout << "DEX: " << DEX << endl;
    	
    	INTgen();
    	cout << "INT: " << INT << endl;
    	
    	STRgen();
    	cout << "STR: " << STR << endl;
    	
    	WISgen();
    	cout << "WIS: " << WIS << endl;
    	
    	feats();
    	cout << "Feats: " << Feat1 << " " << Feat2 << " " << Feat3 << " " << Feat4 << " " << Feat5 << endl;
    
    	traits();
    	cout << "Traits: " << Trait1 << Trait2 << Trait3 << Trait4 << Trait5 << endl;
    	
    	features();
    	
    	spells();
    	
    	special();
    
    	cout << "\n\nHit q to quit: ";
    	cin >> x;
    
    	return;
    }
    Does that make a bit more sense?

    Trying to pass a string from an array (feat1) to a pointer (pf1) to a char global (Feat1).

  6. #6
    Earth_King
    Guest
    Originally posted by Dragonlord


    That would hold true if the array constants were also the names of the variables I was trying to pass to, but here:

    Code:
    #include <iostream.h>
    #include <cstdlib>
    #include <time.h>
    
    void acgen();
    void hpgen();
    void CHRgen();
    void CONgen();
    void DEXgen();
    void INTgen();
    void STRgen();
    void WISgen();
    void feats();
    void traits();
    void features();
    void spells();
    void special();
    
    int AC;
    int HP;
    int CHR;
    int CON;
    int DEX;
    int INT;
    int STR;
    int WIS;
    char Name[50];
    char Feat1;
    char Feat2;
    char Feat3;
    char Feat4;
    char Feat5;
    char Trait1;
    char Trait2;
    char Trait3;
    char Trait4;
    char Trait5;
    char Features;
    char Spells;
    char Special;
    In other words, what I am trying to pass to are just more char type variables, not the arrays themselves.

    Also, in main() you'll see what I mean...

    Code:
    void main()
    {
    	char x;
    
    	cout << "What is the Monster Name? ";
    	cin >> Name;
    	cout << endl;
    
    	acgen();
    	cout << "AC: " << AC << endl;
    
    	hpgen();
    	cout << "HP: " << HP << endl;
    	
    	CHRgen();
    	cout << "CHR: " << CHR << endl;
    	
    	CONgen();
    	cout << "CON: " << CON << endl;
    	
    	DEXgen();
    	cout << "DEX: " << DEX << endl;
    	
    	INTgen();
    	cout << "INT: " << INT << endl;
    	
    	STRgen();
    	cout << "STR: " << STR << endl;
    	
    	WISgen();
    	cout << "WIS: " << WIS << endl;
    	
    	feats();
    	cout << "Feats: " << Feat1 << " " << Feat2 << " " << Feat3 << " " << Feat4 << " " << Feat5 << endl;
    
    	traits();
    	cout << "Traits: " << Trait1 << Trait2 << Trait3 << Trait4 << Trait5 << endl;
    	
    	features();
    	
    	spells();
    	
    	special();
    
    	cout << "\n\nHit q to quit: ";
    	cin >> x;
    
    	return;
    }
    Does that make a bit more sense?

    Trying to pass a string from an array (feat1) to a pointer (pf1) to a char global (Feat1).
    Oh, I see........

    Since Feat1 is a character variable, it will hold a char while pf1 is a pointer that hold an address, you can not do:
    Feat1 =pf1; this is illegal

    But you can do Feat1 = feat[0] or Feat1 = *(pf+0)
    Since feat[0] hold the first character of string array and pf also point to the first character of string array.

    Earth_King

  7. #7
    Hammer
    Guest
    >>>void main()
    Is wrong. Use int main(void)

    Try not to create variables with the same names, the only difference being the case (eg Feat1 and feat1). This only serves to confuse (imo).

    Your code is C++, and should therefore really be posted on the relevant board.

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, 09:18 PM
  2. how do you resolve this error?
    By -EquinoX- in forum C Programming
    Replies: 32
    Last Post: 11-05-2008, 04:35 PM
  3. Can you check what is wrong with this code
    By Ron in forum C++ Programming
    Replies: 4
    Last Post: 08-01-2008, 11:59 PM
  4. Replies: 6
    Last Post: 07-29-2008, 05:37 AM
  5. strings Vs. Char pointers
    By aijazbaig1 in forum C Programming
    Replies: 49
    Last Post: 02-13-2008, 09:51 AM

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