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. 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
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.
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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 () {...
|
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...
|
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...
|
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;
|
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...
|
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...
|
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 *...
|
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;
|
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...
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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...
|
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,...
| |