Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Array of Structure

Posted on 2001-06-25
6
Medium Priority
?
193 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
[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
6 Comments
 
LVL 5

Expert Comment

by:djbusychild
ID: 6226243
#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
ID: 6272099
What if i want a dynamic char instead of int ?
0
 
LVL 5

Expert Comment

by:djbusychild
ID: 6273384
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
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 5

Accepted Solution

by:
djbusychild earned 400 total points
ID: 6274006
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
ID: 6820057
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
ID: 6837755
Accepted question as recommended.

Regards,
ComTech
CS @ EE
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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
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 goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

705 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