Solved

Array of Structure

Posted on 2001-06-25
6
190 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
Independent Software Vendors: 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 100 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Cross compile release version of c++ program for linux 2 146
Error creating a new C++ project in ,net 20 59
maximize the sum of fractions 33 76
Winapi.Windows.hpp problem 7 45
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

738 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