[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Problem in array of structure .??

Posted on 1998-06-01
9
Medium Priority
?
275 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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 240 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
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.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
Suggested Courses

650 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