Solved

Problem in array of structure .??

Posted on 1998-06-01
9
248 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

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 is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

730 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