Solved

Problem in array of structure .??

Posted on 1998-06-01
9
213 Views
Last Modified: 2010-04-15
Hi,

 I am trying to create an array of structures and then
storing things in it. Apparantly, I am getting a Segmentation fault (Even though I am allocating (I think) enough space.

Here is my program:

====Main.c
void call_tofunc2(Tester **KRDL);
main()
{
  Tester *KRDL;
KRDL=(Tester *)malloc(sizeof(Tester) * 2);
call_tofunc2(&KRDL);
}

void call_tofunc2(Tester **KRDL)
{
printf("\nI am in funct2 \n");
strcpy((*KRDL[0]).flag,"This is");
printf("\nJust passed the first strcpy function \n");
strcpy((*KRDL[1]).flag,"a toy");

printf("\nThis is 0 : %s\n",(*KRDL[0]).flag);
printf("\nThis is 1 : %s\n",(*KRDL[1]).flag);
}

===head.h file
typedef struct{
          char flag[30];
          int id[5];
          }Tester;
====
I am getting the following output :
==
I am in funct2

Just passed the first strcpy function
Segmentation Fault
==

Am I allocating the wrong way ??? Any help will be appreciated.

Thanks
0
Comment
Question by:singhtaj
9 Comments
 

Author Comment

by:singhtaj
ID: 1251080
Adjusted points to 60
0
 
LVL 4

Expert Comment

by:sganta
ID: 1251081
The statement should be

KRDL=(Tester *)malloc(sizeof(Tester))
0
 

Author Comment

by:singhtaj
ID: 1251082
That method still doesn't work. By the way , the one you
suggested doesn't create an array of structures. It simply
allocates memory for one single structure, whereas I want
array of structures to be created.

0
 
LVL 4

Expert Comment

by:sganta
ID: 1251083
Then your declaration statement
You can directly declare like this
#include <stdio.h>
---
---
#define MAX 80
Tester KRDL[MAX];

Now you can remove the memory allocation statement i.e( (Tester*)malloc ...).
Because Array will take care of allocating memory.

I hope this will work .
Regards - sganta
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:singhtaj
ID: 1251084
The Problem is I don't know max size

0
 

Author Comment

by:singhtaj
ID: 1251085
Continue... So I can't do what  you suggest.


0
 
LVL 10

Accepted Solution

by:
rbr earned 60 total points
ID: 1251086
Try this

void call_tofunc2(Tester *KRDL);
main()
    {
      Tester *KRDL;
    KRDL=(Tester *)malloc(sizeof(Tester) * 2);
    call_tofunc2(KRDL);
    }

    void call_tofunc2(Tester *KRDL)
    {
    printf("\nI am in funct2 \n");
    strcpy(KRDL[0].flag,"This is");
    printf("\nJust passed the first strcpy function \n");
    strcpy(KRDL[1].flag,"a toy");

    printf("\nThis is 0 : %s\n",KRDL[0].flag);
    printf("\nThis is 1 : %s\n",KRDL[1].flag);
    }

You don't need a ** in your program.
0
 

Author Comment

by:singhtaj
ID: 1251087
Okay, I think I figured it out.

Thanks

0
 
LVL 1

Expert Comment

by:ciao
ID: 1251088
The problems is that Tester is a pointer of structure -> you would allocate the size of a pointer type. Write :

KRDL=(Tester *)malloc(sizeof(*Tester) * 2);
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now