473,396 Members | 1,971 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,396 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 2262

"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...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.