Solved

Problem in array of structure .??

Posted on 1998-06-01
9
234 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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
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 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 switch statements in the C programming language.

810 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