Solved

Array of Structure

Posted on 2001-06-25
6
186 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

910 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

16 Experts available now in Live!

Get 1:1 Help Now