char[x] - strings - passing into functions - need help.

S

Steven Taylor

Hope someone can assist. I'm trying to answer a book question. I'm
going around in circles in relation to 'pointer-to-char'. Object : A
short program is to be created, which involves a structore and one
member element is a char array. A function is to be created that
passes the structure as a reference, along with the 'pointer-to-char'.
The function is to assign the passed-in 'pointer-to-char' to the
referenced (passed-in) structure. The compiler error messages are such
as 'incompatible char to char[20]'. Here is my attempt below.

// C++ Primer Plus, Fifth Edition
// Chapter 8, Question 2, page 390.
// Q-08-02.cpp
#include <iostream>
struct candybar
{
char brand[20];
double weight;
int cal;
};
void pop(candybar & snack, const char *str = "Millennium Munch", const
double w = 2.85, const int c = 350);

int main()
{
using namespace std;
candybar meal;

cout << "Only passing one reference:\n";
pop(meal);

cout << "Now passing all references, overriding default values:\n";
pop(meal,"Crunchie Bar",4.50,500);
return 0;
}
void pop(candybar &snack, const char *str, const double w, const int c)
{
using namespace std;
snack.brand = *str; // compile error here
snack.weight = w;
snack.cal = c;
cout << "Brand : " << snack.brand << endl;
cout << "Weight : " << snack.weight << endl;
cout << "Calories : " << snack.cal << endl;
}

Steven Taylor
Melbourne, Australia.
 
S

Sharad Kala

Steven Taylor said:
void pop(candybar &snack, const char *str, const double w, const int c)
{
using namespace std;
snack.brand = *str; // compile error here

Arrays are not assignable, use strcpy family of functions instead.
snack.weight = w;
snack.cal = c;

Sharad
 
S

Steven Taylor

Sharad said:
Arrays are not assignable, use strcpy family of functions instead.
Thanks for that, I ended up with
strcpy(snack.brand, str); (include <cstring> at the start)

Much appreciated.

Steve.
 
H

hibiki

Steven Taylor a écrit :
Hope someone can assist. I'm trying to answer a book question. I'm
going around in circles in relation to 'pointer-to-char'. Object : A
short program is to be created, which involves a structore and one
member element is a char array. A function is to be created that
passes the structure as a reference, along with the 'pointer-to-char'.
The function is to assign the passed-in 'pointer-to-char' to the
referenced (passed-in) structure. The compiler error messages are such
as 'incompatible char to char[20]'. Here is my attempt below.

// C++ Primer Plus, Fifth Edition
// Chapter 8, Question 2, page 390.
// Q-08-02.cpp
#include <iostream>
struct candybar
{
char brand[20];
double weight;
int cal;
};
void pop(candybar & snack, const char *str = "Millennium Munch", const
double w = 2.85, const int c = 350);

int main()
{
using namespace std;
candybar meal;

cout << "Only passing one reference:\n";
pop(meal);

cout << "Now passing all references, overriding default values:\n";
pop(meal,"Crunchie Bar",4.50,500);
return 0;
}
void pop(candybar &snack, const char *str, const double w, const int c)
{
using namespace std;
snack.brand = *str; // compile error here
snack.weight = w;
snack.cal = c;
cout << "Brand : " << snack.brand << endl;
cout << "Weight : " << snack.weight << endl;
cout << "Calories : " << snack.cal << endl;
}

Steven Taylor
Melbourne, Australia.
You can't copy a char* string to another char* string by using the '='
operator.
You must write strcpy(char*, char*);
If you put *str, you take the first char of the string, not a reference
to a char* object (Unlike java, base types and arrays are not objects)

--
Salutations,

Joachim Naulet

06 14 90 06 21
http://jnaulet.no-ip.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top