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
Solved

Problem in array of structure .??

Posted on 1998-06-01
9
240 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

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.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

860 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