|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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?
Quote:
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
|
||||
|
||||
|
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. |
|
#3
|
||||
|
||||
|
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 02:36 PM. |
|
#4
|
|||
|
|||
|
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; !!!!!!!! |
|
#5
|
|||
|
|||
|
Quote:
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. |
|
#6
|
|||
|
|||
|
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 06:18 PM. |
|
#7
|
||||
|
||||
|
Quote:
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. |
|
#8
|
|||
|
|||
|
Quote:
isn't that a non-existant thing? |
|
#9
|
|||
|
|||
|
Quote:
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. |
|
#10
|
||||
|
||||
|
Change this line
Code:
if(isNumeric(*chExample)==true){
to: Code:
if(isNumeric(chExample)==true){
|
|
#11
|
||||
|
||||
|
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.] |
|
#12
|
|||
|
|||
|
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 ![]() |
|
#13
|
||||
|
||||
|
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. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > C Programming > Having problems with my pointers - new to them |
| Thread Tools |