Solved

Array of Structure

Posted on 2001-06-25
6
185 Views
Last Modified: 2010-08-05
Hello,

Does anyone know how to create array of struct
and pass into a function and how to access it
in the functin ?
0
Comment
Question by:g_temp
6 Comments
 
LVL 5

Expert Comment

by:djbusychild
Comment Utility
#include <stdio.h>

typedef struct mystruct {
     int i;
} mystruct_st;


void func(mystruct_st* user_struct, int len)
{
     int i;
     for (i = 0;i<len;i++) {
          printf("struct %d = %d\n",i,user_struct[i].i);
     }
}

int main(int argc, char* argv[])
{
     int i;
     mystruct_st struct_arr[5];
     
     for (i = 0;i<5;i++) {
          struct_arr[i].i = i;
     }

     func(struct_arr,5);

     return 0;
}
0
 

Author Comment

by:g_temp
Comment Utility
What if i want a dynamic char instead of int ?
0
 
LVL 5

Expert Comment

by:djbusychild
Comment Utility
same thing

just use char* instead of int
and malloc character array

#include <stdio.h>

typedef struct mystruct {
    char* i;
} mystruct_st;


void func(mystruct_st* user_struct, int len)
{
    int i;
    for (i = 0;i<len;i++) {
         printf("struct %d = %d\n",i,user_struct[i].i);
    }
}

int main(int argc, char* argv[])
{
    int i;
    mystruct_st struct_arr[5];
   
    for (i = 0;i<5;i++) {
         struct_arr[i].i = (*char)malloc(256);
    }

    func(struct_arr,5);

    return 0;
}
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

 
LVL 5

Accepted Solution

by:
djbusychild earned 100 total points
Comment Utility
oops... I didn't put anything into the buffer. =)

right now the program will most probably crash depending on what garbage is in the buffer.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

typedef struct mystruct {
   char* i;
} mystruct_st;


void func(mystruct_st* user_struct, int len)
{
   int i;
   for (i = 0;i<len;i++) {
        printf("struct %d = %s\n",i,user_struct[i].i);
   }
}


int main(int argc, char* argv[])
{
   int i;
   mystruct_st struct_arr[5];
   
   for (i = 0;i<5;i++) {
        struct_arr[i].i = (char*)malloc(256);
        strcpy(struct_arr[i].i,"hello world");
   }

   func(struct_arr,5);

   return 0;
}
0
 
LVL 11

Expert Comment

by:griessh
Comment Utility
I think you forgot this question. I will ask Community Support to close it unless you finalize it within 7 days. Unless there is objection or further activity,  I will suggest to accept "djbusychild" comment(s) as an answer.

If you think your question was not answered at all, you can post a request in Community support (please include this link) to refund your points.
The link to the Community Support area is: http://www.experts-exchange.com/jsp/qList.jsp?ta=commspt

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
======
Werner

0
 

Expert Comment

by:ComTech
Comment Utility
Accepted question as recommended.

Regards,
ComTech
CS @ EE
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

772 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

10 Experts available now in Live!

Get 1:1 Help Now