Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 04-18-2005, 07:59 AM   #1 (permalink)
JaySun
Registered User
 
Join Date: Mar 2005
Posts: 7
JaySun is on a distinguished road
Question Structures and Arrays in C

I am supposed to create a structure and an array that will hold 4 occurences of the structure. I am having a difficult time with assigning the values to the array. Yes this is a class assignment, I really do not need to hear about hurricanes these days! Here is a bit of the code:
Code:
 #include <stdio.h>
 
 int main (void)
 {
 	struct hurricane
	{
		char hurricane_name[10];
		int category;
		int max_windspeed;
		char month[10];
		int day;
		char landfall[20];
	}h[4];
	
	h[0].hurricane_name[10] = "Charley";
	h[0].category = 4;
	h[0].max_windspeed = 145;
	h[0].month = "August";
	h[0].day = 13;
	h[0].landfall = "Charlotte Co.";
     
        return 0;
}
The code further goes on to add 3 more hurricanes into the array and then just print them out (which I have no issue with). Why is my syntax wrong. As always help is appreciated!
JaySun is offline   Reply With Quote
Old 04-18-2005, 09:05 AM   #2 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
In C or C++?
__________________
Valmont is offline   Reply With Quote
Old 04-18-2005, 11:23 AM   #3 (permalink)
JaySun
Registered User
 
Join Date: Mar 2005
Posts: 7
JaySun is on a distinguished road
C
JaySun is offline   Reply With Quote
Old 04-18-2005, 11:45 AM   #4 (permalink)
JaySun
Registered User
 
Join Date: Mar 2005
Posts: 7
JaySun is on a distinguished road
I tried using strcpy to store the values of the strings into the array and that seems to be working.

Thanks
JaySun is offline   Reply With Quote
Old 04-19-2005, 01:04 AM   #5 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,720
redhead is on a distinguished road
Here is one in C, try to compare it with yours and see where your assignments went wrong.
Code:
#include <stdio.h>

typedef struct hurricane
{
  char hurricane_name[10];
  int category;
  int max_windspeed;
  char month[10];
  int day;
  char landfall[20];
}hurricane;


int main (void)
{
  hurricane h[4];
  strcpy(h[0].hurricane_name,"Charley");
  h[0].category = 4;
  h[0].max_windspeed = 145;
  strcpy(h[0].month, "August");
  h[0].day = 13;
  strcpy(h[0].landfall, "Charlotte Co.");

  /* see if it was filled correct */
  printf("Hurricane name: %s\n", h[0].hurricane_name);
  printf("Hurricane category: %d\n", h[0].category);
  printf("Hurricane max_windspeed: %d\n", h[0].max_windspeed);
  printf("Hurricane month: %s\n", h[0].month);
  printf("Hurricane day: %d\n", h[0].day);
  printf("Hurricane landfall: %s\n", h[0].landfall);
  return 0;
}
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

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 On
Refbacks are On



All times are GMT -8. The time now is 01:22 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting