#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Posts
    14
    Rep Power
    0

    Question Having problems with my pointers - new to them


    I'm new with pointers, and I'm obviously using something wrong, but I don't know what. Anyone care to shed some light on this for me?

    h:\cp132\hw25\receipt.cpp(58) : error C2440: '=' : cannot convert from 'char *' to 'char *[20]'
    There are no conversions to array types, although there are conversions to references or pointers to arrays
    h:\cp132\hw25\receipt.cpp(59) : error C2440: '=' : cannot convert from 'char *' to 'char *[12]'
    There are no conversions to array types, although there are conversions to references or pointers to arrays
    structure in groceryinfo.h for reference below:
    Code:
    struct item_type{		//info for each item
    	char *chName[20];	//name of the item
    	char *chUPC[12];		//UPC label of the item
    	double *dPrice;		//price of the item
    	int *iQuan;			//in-stock quantity of the item
    };
    function call from groceryinfo.cpp:
    Code:
    //grocery is the name of the structure
    receipt GrocPurchase(*grocery.chName, *grocery.chUPC, *grocery.dPrice, *grocery.iQuan);

    receipt.h:
    Code:
    class receipt{						//class definition
    
    	public:							//accessible outside the class
    		receipt(void);				//default constructor
    		
    		//constructor that recieves groc info
                                    //THIS IS WHAT IS HAVING PROBLEMS
    		receipt(char tmpName[], char tmpUPC[], double tmpPrice, int tmpQuan);
    		void show_receipt(void);	//shows user's receipt
    		void operator +=(receipt &info);//operator to store item info to vector
    
    	private:						//accessible only within the class
    
    		//structure for item info
    		struct item_type{		//info for each item
    			char *chName[20];	//name of the item
    			char *chUPC[12];	//UPC label of the item
    			double *dPrice;		//price of the item
    			int *iQuan;			//in-stock quantity of the item
    		};
    		
    		item_type usergrocery;
    
    		vector<receipt> vReceipt;	//vector to store receipt info
    
    };									//closes the class definition

    receipt.cpp:
    Code:
    receipt::receipt(char tmpName[20], char tmpUPC[12], double tmpPrice, int tmpQuan){
    
    	item_type grocery;		//structure to store grocery information to
    
    	//creates memory allocations for the pointers
                    //HAVING PROBLEMS HERE
    	grocery.chName=new char[20];
    	grocery.chUPC=new char[12];
    	grocery.dPrice=new double;
    	grocery.iQuan=new int;
    
    	strcpy(*grocery.chName,tmpName);
    	strcpy(*grocery.chUPC,tmpUPC);
    	*grocery.dPrice=tmpPrice;
    	*grocery.iQuan=tmpQuan;
    }
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,648
    Rep Power
    4248
    Change your groceryinfo.h declaration as follows:
    Code:
    struct item_type{		//info for each item
    	char *chName;	//name of the item
    	char *chUPC;		//UPC label of the item
    	double *dPrice;		//price of the item
    	int *iQuan;			//in-stock quantity of the item
    };
    The way you had it before (i.e. char *chName[20]) declares an array of 20 pointers, not a pointer to an array of 20 chars.
  4. #3
  5. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,255
    Rep Power
    2222
    Also remove that extra asterix used with chName and chUPC in your function call and in the receipt constructor.

    Question: why are dPrice and iQuan pointers? chName and chUPC need to be because they point to arrays. What advantage is there to going through that extra processing with dPrice and iQuan?

    There is a richly expressive complexity to pointer declarations that is rarely covered adequately. You can have several levels of indirection (ie, pointers to pointers to pointers). A very good description was in Chapter 10 of "Schaum's Outline of Programming with C".
    Last edited by dwise1_aol; May 19th, 2003 at 03:36 PM.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Location
    turkey
    Posts
    15
    Rep Power
    0
    h:\cp132\hw25\receipt.cpp(58) : error C2440: '=' : cannot convert from 'char *' to 'char *[20]'

    i think,here u are trying to change address of an array!! like this

    char str[20];
    char *p;

    p is getting any value in code; and later u r doing maybe sth like this

    str=p; !!!!!!!!
  8. #5
  9. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Posts
    14
    Rep Power
    0
    Originally posted by dwise1_aol
    Also remove that extra asterix used with chName and chUPC in your function call and in the receipt constructor.

    Question: why are dPrice and iQuan pointers? chName and chUPC need to be because they point to arrays. What advantage is there to going through that extra processing with dPrice and iQuan?

    There is a richly expressive complexity to pointer declarations that is rarely covered adequately. You can have several levels of indirection (ie, pointers to pointers to pointers). A very good description was in Chapter 10 of "Schaum's Outline of Programming with C".
    I don't know why - we're just required to in my class, that's all I know. I personally don't see why either. I think he just wants us to get the feel of pointers.
  10. #6
  11. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Posts
    14
    Rep Power
    0
    Is there any quick, plausible way to convert a pointer to a character (array) to an actual, non-pointer character array? The errorchecking functions toFloat and isNumeric we're required to use are coded to receive a character array, but don't function properly if they're receiving a pointer.

    By the way, thanks guys, the suggestions on the pointers fixed it ;)
    Last edited by homncruse; May 19th, 2003 at 07:18 PM.
  12. #7
  13. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,255
    Rep Power
    2222
    Originally posted by homncruse
    Is there any quick, plausible way to convert a pointer to a character (array) to an actual, non-pointer character array? The errorchecking functions toFloat and isNumeric we're required to use are coded to receive a character array, but don't function properly if they're receiving a pointer.
    That doesn't sound right. Could you please show an example that works and one that doesn't? And the prototypes of toFloat and of isNumeric too.
  14. #8
  15. No Profile Picture
    .
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2002
    Posts
    296
    Rep Power
    13
    non-pointer character array
    isn't that a non-existant thing?
  16. #9
  17. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Posts
    14
    Rep Power
    0
    Originally posted by dwise1_aol
    That doesn't sound right. Could you please show an example that works and one that doesn't? And the prototypes of toFloat and of isNumeric too.
    Prototypes:

    bool isNumeric(char theString[]);
    float toFloat(char theString[]);

    Works:
    Code:
    int iExample;
    char chExample[5];
    strcpy(chExample, "1234");
    if(isNumeric(chExample)==true){
        iExample=toFloat(chExample);
    }
    Doesn't work:
    Code:
    char *chExample;
    //blah blah blah, populate chExample
    if(isNumeric(*chExample)==true){  //function cannot convert apparently
    //blah blah blah, same error with toFloat
    Sorry if the code is a little off, I just whipped that out in the post, not an actual "working" example, but similar to what I'd (normally) use.
    I've tried taking the brackets off the prototypes and have it receive a pointer, but then it gives me a whole new set of errors.
  18. #10
  19. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,648
    Rep Power
    4248
    Change this line
    Code:
    if(isNumeric(*chExample)==true){
    to:
    Code:
    if(isNumeric(chExample)==true){
  20. #11
  21. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,255
    Rep Power
    2222
    Now, by way of explanation of Scorpions' reply:

    Your prototypes:
    bool isNumeric(char theString[]);
    float toFloat(char theString[]);

    could also have been written as:
    bool isNumeric(char *theString);
    float toFloat(char *theString);

    Remember: pointer, array name, same thing!

    So both functions are expecting a character pointer or a char array name, which of course are the same thing.

    When you declare a pointer, you use the asterix thus:
    char *chExample;
    From then on, whenever a char pointer is needed, you simply use the variable as-is: chExample. Which is why the first example you gave works:
    if(isNumeric(chExample)==true){
    iExample=toFloat(chExample);

    Now, when you want to access the actual data that a pointer is pointing to, you use the asterix again, but its meaning is entirely different. Now it means "dereference this pointer". So, since chExample is a char pointer, *chExample refers directly to the single character that it is pointing to, the first character and only the first character in that character array. Which is not what you really want to do here, nor is it what the function expects.

    Keep this in mind:
    chExample is of type char* -- [char pointer]
    *chExample is of type char
    isNumeric expects to be passed a char*, not a char

    It helps if you read your code in terms of what you are telling the computer to do. As if C were a foreign language that you are trying to communicate through, think of the meaning of each of your statements. Don't take the Berlitz approach of memorizing a million and one sentences without understanding just what they say or how they are constructed, but rather do try to understand how the statements' and expressions' syntax help to convey the meaning that you want to communicate.

    [BTW, I was a foreign language student before I started programming and I started off using the same approach with great success to programming languages as I had with human languages.]
  22. #12
  23. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Posts
    14
    Rep Power
    0
    Thank ya' kindly. I'll give that a shot when I'm in class tomorrow, and see if that fixes it. My teacher isn't a guru and doesn't know how to fix it either - he's really learning from us as much as we're learning from him :P
    In fact, he didn't even know about system("pause") or a few other simple things ;)
  24. #13
  25. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,255
    Rep Power
    2222
    One of the best ways to learn something is to teach it. As long as the instructor knows a more than the students and doesn't make dogmatic statements about what is and is not possible to do (unless he does know it for sure), both students and instructor should benefit.

    Years ago I was told that in one infamous case of an imposter who posed as several different professionals, he said that the easiest to fake was a college professor. All he had to do was read two chapters ahead of the students. No, I never was told what subject.

    Actually, I have 12 years experience with C/C++ (mostly C these past 8 years) and am considered an expert at it here at work. In the past six months on this forum I've been learning things about C that I hadn't known before, including the system() function. Most of us program within our comfort zones with the tools and techniques we're familiar with and don't usually explore all the other tools that are available.

IMN logo majestic logo threadwatch logo seochat tools logo