473,396 Members | 1,783 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Array of structures in C

thatos
105 100+
I'm trying to create a structure which has an integer and a pointer to the same type. So I created a structure of that type but I seem to be printing wrong values.
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <string.h>
  3. #define max 20
  4. struct node{
  5.     int i;
  6.     struct node *next;
  7. };
  8. void print(struct node*);
  9.  
  10. int main(){
  11.     struct node* b= (struct node*) malloc(sizeof(struct node));
  12.     b[0].i = 1;
  13.     b[0].next = NULL;
  14.     b[1].i = 2;
  15.     b[1].next = NULL;
  16.     struct node *c = (struct node*) malloc(sizeof(struct node));
  17.     c[0].i = 3;
  18.     c[0].next = NULL;
  19.     c[1].i = 4;
  20.     c[1].next = NULL;
  21.     b[0].next = c;
  22.     //print(b);
  23.     printf("%d %d\n",b[0].i,b[1].i);
  24.     return 1;
  25. }
  26.  
My ideal output should be
Expand|Select|Wrap|Line Numbers
  1. 1 2
  2.  
but I'm getting
Expand|Select|Wrap|Line Numbers
  1. 1 3
  2.  
I can't figure out why this is happening, please help
Jul 20 '11 #1
1 1454
mac11
256 100+
The malloc's are allocating enough for one node, but the code acts like it's got an array of nodes.

"b" is one node, "c" is one node. There is no b[1] or c[1]. You should access b using pointer notation and not confuse yourself

b->i = 1;
b->next = NULL;
Jul 20 '11 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: siliconmike | last post by:
Hello, I'd like to implement a large 2-d array 2d array size: 5000 x 5000 each element: int 4 bytes type: static, MYISAM how is this done in MySQL? I'm a newbie, so details will help. Mike
0
by: Wayne Wengert | last post by:
I have a basic understanding of XML and I have a similar basic grasp of VB.NET for developing Windows applications. I am still struggling with the when/where/how in the proper use of OO classes and...
2
by: kittu_phani | last post by:
Iam very much new to C-programing.Please give the silution for following problem. How to read data from stdin to array ofstructures -- Posted via http://dbforums.com
4
by: emma middlebrook | last post by:
Hi Straight to the point - I don't understand why System.Array derives from IList (given the methods/properties actually on IList). When designing an interface you specify a contract. Deriving...
20
by: Luke Matuszewski | last post by:
Welcome As suggested i looked into JSON project and was amazed but... What about cyclical data structures - anybody was faced it in some project ? Is there any satisactional recomendation... ...
104
by: Leszek | last post by:
Hi. Is it possible in javascript to operate on an array without knowing how mamy elements it has? What i want to do is sending an array to a script, and this script should add all values from...
7
by: Sam | last post by:
Hello I have a structure called Company. struct Company { char *employee; char *employee_address; }; I want to build an array of this structure but the number of employees will change...
44
by: svata | last post by:
Hello, I wonder how to resize such array of structures using realloc()? #include <stdio.h> #include <stdlib.h> #define FIRST 7 typedef struct { char *name;
10
by: padh.ayo | last post by:
Hi, I have a array structure called people It is properly initialized to 100 array structure elements. Now, I'm reading in from the command line a txt file, and I get them to open correctly. In...
5
by: =?Utf-8?B?QXlrdXQgRXJnaW4=?= | last post by:
Hi Willy, Thank you very much for your work. C++ code doesnot make any serialization. So at runtime C# code gives an serialization error at "msg_file_s sa = (msg_file_s) bf.Deserialize(ms);"...
0
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
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
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,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
tracyyun
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...
0
agi2029
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,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.