Skip to content

Instantly share code, notes, and snippets.

@mahata
Created September 26, 2011 03:45
Show Gist options
  • Save mahata/1241566 to your computer and use it in GitHub Desktop.
Save mahata/1241566 to your computer and use it in GitHub Desktop.
C++ Primer 8.2
#include <iostream>
#include <cstring>
const int MAX_STR_LEN = 255;
struct CandyBar
{
char brand[MAX_STR_LEN];
double weight;
int calorie;
};
void set_candy_bar(CandyBar & candy_bar, const char * brand, double weight, int calorie);
void show_candy_bar(const CandyBar & candy_bar);
int main()
{
CandyBar sample_candy_bar = {"Millenium Munch", 2.85, 350};
show_candy_bar(sample_candy_bar);
set_candy_bar(sample_candy_bar, "New Munch", 3.0, 400);
show_candy_bar(sample_candy_bar);
return 0;
}
void set_candy_bar(CandyBar & candy_bar, const char * brand, double weight, int calorie)
{
strncpy(candy_bar.brand, brand, sizeof(candy_bar.brand) -1);
candy_bar.weight = weight;
candy_bar.calorie = calorie;
}
void show_candy_bar(const CandyBar & candy_bar)
{
using namespace std;
cout << "-- output --" << endl;
cout << "brand: " << candy_bar.brand << endl;
cout << "weight: " << candy_bar.weight << endl;
cout << "calorie: " << candy_bar.calorie << endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment