There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
 
Tag Cloud
access audio avg avg 8 bios blue screen boot bsod computer connection cpu crash css dell desktop dma driver drivers dvd email error excel explorer firefox firefox 3 freeze gimp graphics hard drive hardware hijackthis hjt install internet internet explorer itunes keyboard laptop macro malware monitor motherboard network networking outlook outlook 2003 outlook 2007 outlook express pio problem problems router seo server slow sound sp3 spyware trojan usb video virtumonde virus vista vundo windows windows vista windows xp winxp wireless
Software Development
Search
Search in:
 
Advanced Search
Tech Support Guy Forums > Software & Hardware > Software Development >
Inputting data to a structure (C)


HELLO AND WELCOME! Before you can post your question, you'll have to register -- it's completely free! Click here to join today! We highly recommend that you print a copy of our Guide for New Members. Enjoy!

 
Thread Tools
Villanz's Avatar
Member with 99 posts.
 
Join Date: May 2004
Location: West Midlands, UK
Experience: Intermediate
28-Oct-2005, 02:12 PM #1
Inputting data to a structure (C)
Could someone please tell me what is wrong with this statement?

person.name = "Dan";

...where 'person' is the name of the structure and 'name' is the name of a string variable within it.
The compiler says something about incompatible types in assignment.
AGCurry's Avatar
Senior Member with 431 posts.
 
Join Date: Jun 2005
Location: Kansas City area
Experience: advanced but learning
28-Oct-2005, 03:33 PM #2
It depends on whether name is an array of characters or a pointer to char.

If name is declared as char *name, then your assignment is okay.

If it's declared as char name[20], it's not.
Villanz's Avatar
Member with 99 posts.
 
Join Date: May 2004
Location: West Midlands, UK
Experience: Intermediate
28-Oct-2005, 06:27 PM #3
Thanks for the reply.
It's declared as char name[20]. Is there a way i can make it work like that or will i have to use a pointer?
coderitr's Avatar
Distinguished Member with 3,080 posts.
 
Join Date: Oct 2003
29-Oct-2005, 03:52 PM #4
strcpy( person.name, "Dan" );
IMM's Avatar
IMM IMM is offline
Distinguished Member with 3,157 posts.
 
Join Date: Feb 2002
29-Oct-2005, 09:23 PM #5
I'm not recommending it in this case - but if I want to have a char pointer to a fixed size buffer at compile time in C - I'll usu. do the following as the declaration - giving me both methods so that I can address it with the char* or as the array

Code:
#define BUFFER_SIZE 20 
  char name_buf[BUFFER_SIZE], *name_ptr = name_buf;
This is an easy one to make serious mistakes with tho'
AGCurry's Avatar
Senior Member with 431 posts.
 
Join Date: Jun 2005
Location: Kansas City area
Experience: advanced but learning
31-Oct-2005, 09:37 AM #6
Quote:
Originally Posted by Villanz
Thanks for the reply.
It's declared as char name[20]. Is there a way i can make it work like that or will i have to use a pointer?
struct whatever
{
char *name ;
char *phone ;
} ;

.
.
.
struct whatever person ;

person.name = "Dan" ;
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are Off
Refbacks are Off

You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 05:29 AM.
Copyright © 1996 - 2008 TechGuy, Inc. All rights reserved.
Powered by vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Powered by Cermak Technologies, Inc.