473,379 Members | 974 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

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

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.

Jul 23 '05 #1
3 2261

"Steven Taylor" <ne******@superjacent.net> wrote in message
[snip]
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
Jul 23 '05 #2
Sharad Kala wrote:
"Steven Taylor" <ne******@superjacent.net> wrote in message
[snip]
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.

Thanks for that, I ended up with
strcpy(snack.brand, str); (include <cstring> at the start)

Much appreciated.

Steve.
Jul 23 '05 #3
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
Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

30
by: Tim Johansson | last post by:
I'm new to C++, and tried to start making a script that will shuffle an array. Can someone please tell me what's wrong? #include <iostream.h> #include <string.h> int main () {...
8
by: Ekim | last post by:
my question is as follows: I've got a DLL in which I have a method GetBuffer (this one is extern, exported, is called from outside this program) which shall pass a char-buffer to the...
2
by: Peter Nilsson | last post by:
In a post regarding toupper(), Richard Heathfield once asked me to think about what the conversion of a char to unsigned char would mean, and whether it was sensible to actually do so. And pete has...
28
by: Merrill & Michele | last post by:
#include <stdio.h> #include <string.h> #include <stdlib.h> int main(void){ char *p; p=malloc(4); strcpy(p, "tja"); printf("%s\n", p); free(p); return 0;
5
by: jab3 | last post by:
(again :)) Hello everyone. I'll ask this even at risk of being accused of not researching adequately. My question (before longer reasoning) is: How does declaring (or defining, whatever) a...
4
by: John Devereux | last post by:
Hi, I would like some advice on whether I should be using plain "chars" for strings. I have instead been using "unsigned char" in my code (for embedded systems). In general the strings contain...
5
by: Jason Bell | last post by:
I'm an experienced programmer, but a newbie to c#, so please be kind :) In a c++, unmanaged, DLL of my own making I have a function called void XGL_Core_SetCaption(XGL_Core * core, char *...
18
by: Pedro Pinto | last post by:
Hi there once more........ Instead of showing all the code my problem is simple. I've tried to create this function: char temp(char *string){ alterString(string); return string;
13
by: Superman859 | last post by:
Hello everyone. Heads up - c++ syntax is killing me. I do quite well in creating a Java program with very few syntax errors, but I get them all over the place in c++. The smallest little things...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.