Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 119,059 C++ Programmers for FREE! Ask your question and get quick answers from experts. There are 1,490 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



Accessing a field within a struct

 
Reply to this topicStart new topic

Accessing a field within a struct

HulkingNightcrawler
post 7 Aug, 2008 - 08:35 PM
Post #1


New D.I.C Head

*
Joined: 26 Jun, 2008
Posts: 20

I am trying to set the name of a student in a struct. But every time I try to do it I get an error of "incompatible types in assignment" and I don't understand why.


CODE

struct studentType
{
char name[20];
int age;
int id;
float gpa;
};

studentType student;
student.name = "Andy";
User is offlineProfile CardPM

Go to the top of the page


KYA
post 7 Aug, 2008 - 09:37 PM
Post #2


#include <nerd.h>

Group Icon
Joined: 14 Sep, 2007
Posts: 3,145



Thanked 24 times

Dream Kudos: 1150
My Contributions


Make it a pointer to a char array like this:

cpp

#include <iostream>
using namespace std;

struct studentType
{
char* name;
int age;
int id;
float gpa;
};

int main()
{
studentType student;
student.name = "Andy";
cout << student.name;
return 0;
}


You couldn't do the earlier version because of array size difference since they are const.
User is offlineProfile CardPM

Go to the top of the page

fountainoftruth
post 7 Aug, 2008 - 11:32 PM
Post #3


D.I.C Head

Group Icon
Joined: 4 Dec, 2007
Posts: 65


My Contributions


Usually, you'll want to do like KYA did for any array inside of a structure that you'll set at run time. Unless, of course, it's something where you know how large it'll have to be no matter what. Names usually aren't like that.
User is offlineProfile CardPM

Go to the top of the page

NickDMax
post 8 Aug, 2008 - 09:31 AM
Post #4


2B||!2B

Group Icon
Joined: 18 Feb, 2007
Posts: 2,763



Thanked 37 times

Dream Kudos: 525
My Contributions


This is how you would do this:
cpp
#include <iostream>
#include <cstring>
using namespace std;

typedef struct
{
char name[20];
int age;
int id;
float gpa;
} studentType;

int main() {
studentType student;
strcpy(student.name, "Andy");
cout << "Student Name: " << student.name << endl;
system("pause");
return 0;
}


There are several problems with just using the pointer.
#1. The memory allocated to hold the data is not inside the structure, which means that operations that do things like copy the data become complicated.

#2. If you assign the pointer to a const char * (like any literal string) then you can not manipulate it. For example student.name[0]=0 is a quick way to set the name to an empty string, but this would cause the program to crash if the pointer pointed to a string literal.

#3. Operations to write the data to a file as a record are complicated since the pointer value may change (it is never a good idea to write a pointer to a file).

There ARE reasons to use pointers in structures, dynamic/arbitrary size for one.
User is offlineProfile CardPM

Go to the top of the page

KYA
post 8 Aug, 2008 - 02:22 PM
Post #5


#include <nerd.h>

Group Icon
Joined: 14 Sep, 2007
Posts: 3,145



Thanked 24 times

Dream Kudos: 1150
My Contributions


The strcpy works, assuming that the input does not ever or could not ever exceed the established array size.
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 10/13/08 03:47PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month