C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old May 19th, 2003, 01:02 PM
homncruse homncruse is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2003
Posts: 14 homncruse User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation 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?

Quote:
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;
}

Reply With Quote
  #2  
Old May 19th, 2003, 01:36 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 9th Plane (9000 - 9499 posts)
 
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
Posts: 9,390 Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 4 Weeks 1 Day 22 h 32 m 40 sec
Reputation Power: 4080
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.

Reply With Quote
  #3  
Old May 19th, 2003, 02:30 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,142 dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 4 Days 20 m 48 sec
Reputation Power: 1974
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.

Reply With Quote
  #4  
Old May 19th, 2003, 03:29 PM
PCPC PCPC is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2003
Location: turkey
Posts: 15 PCPC User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 28 m 49 sec
Reputation 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; !!!!!!!!

Reply With Quote
  #5  
Old May 19th, 2003, 05:15 PM
homncruse homncruse is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2003
Posts: 14 homncruse User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Quote:
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.

Reply With Quote
  #6  
Old May 19th, 2003, 06:11 PM
homncruse homncruse is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2003
Posts: 14 homncruse User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation 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 06:18 PM.

Reply With Quote
  #7  
Old May 19th, 2003, 08:24 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,142 dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 4 Days 20 m 48 sec
Reputation Power: 1974
Quote:
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.

Reply With Quote
  #8  
Old May 19th, 2003, 09:06 PM
balance balance is offline
.
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2002
Posts: 296 balance User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 11
Quote:
non-pointer character array

isn't that a non-existant thing?

Reply With Quote
  #9  
Old May 20th, 2003, 04:32 PM
homncruse homncruse is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2003
Posts: 14 homncruse User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Quote:
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.

Reply With Quote
  #10  
Old May 20th, 2003, 04:57 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 9th Plane (9000 - 9499 posts)
 
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
Posts: 9,390 Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 4 Weeks 1 Day 22 h 32 m 40 sec
Reputation Power: 4080
Change this line
Code:
if(isNumeric(*chExample)==true){  

to:
Code:
if(isNumeric(chExample)==true){  

Reply With Quote
  #11  
Old May 20th, 2003, 05:30 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,142 dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 4 Days 20 m 48 sec
Reputation Power: 1974
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.]

Reply With Quote
  #12  
Old May 21st, 2003, 05:17 PM
homncruse homncruse is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2003
Posts: 14 homncruse User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation 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

Reply With Quote
  #13  
Old May 21st, 2003, 07:07 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,142 dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 4 Days 20 m 48 sec
Reputation Power: 1974
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Having problems with my pointers - new to them

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap