Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Array of Structure

Posted on 2001-06-25
6
Medium Priority
?
194 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
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

[Webinar] Cloud Security

In this webinar you will learn:

-Why existing firewall and DMZ architectures are not suited for securing cloud applications
-How to make your enterprise “Cloud Ready”, and fix your aging DMZ architecture
-How to transform your enterprise and become a Cloud Enabler

Question has a verified solution.

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

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…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses

916 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