473,804 Members | 3,156 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

passing array of structures to/from function

10 New Member
Hello.

I'm pretty new to C, and having a problem which I suspect this has to do with how I'm handling (or not handling) pointers. I have an array of dirent structures which I am passing to a function. The function fills the structures with information on subdirectories in a given directory.

In the function itself, I can step through the array of structures and print to the console the list of directory names. However, when I try to do so from the calling program I get a Segmentation Fault. Snippets from my code are posted below.

Can you tell me what I'm doing wrong? Thanks!
Expand|Select|Wrap|Line Numbers
  1. #include <dirent.h>
  2.  
  3. int main(void)
  4. {
  5. struct dirent     **sEvents;        // structure containing event directory names
  6. int        iResult;        // return value
  7. int        iDirMax;        // number of event directories
  8. int        iDirCtr;        // directory counter
  9. int        GetDirectoryList();    // function to return list of directories
  10.  
  11. // get directory list
  12. iResult = GetDirectoryList(&sEvents, &iDirMax);
  13.  
  14. fprintf(stdout, "Number of Directories = %d\n", iDirMax);
  15.  
  16. for (iDirCtr = 0; iDirCtr < iDirMax; iDirCtr++)
  17.     {
  18.     // THIS GIVES Segmentation Fault..................................
  19.     fprintf(stdout, "Event Directory: %s\n", sEvents[iDirCtr]->d_name);
  20.     }
  21.  
  22. return 0;
  23. }
  24.  
  25. int GetDirectoryList(struct dirent **sEvents, int *iMax)
  26. {
  27. char        cSOD[512];        // path to parent directory
  28. int        iCtr;            // counter
  29.  
  30. // get the list of event directories
  31. *iMax = scandir (cSOD, &sEvents, DirFilter, alphasort);
  32. if (*iMax >= 0)
  33.     {
  34.     for (iCtr = 0; iCtr < *iMax; iCtr++)
  35.         {
  36.         // THIS PRINTS OK...........................................
  37.         fprintf(stdout, "EventDirectory %s\n", sEvents[iCtr]->d_name);
  38.         }
  39.     }
  40.  
  41. return 0;
  42. }
  43.  
  44.  
Jul 12 '07
13 3248
ElderGeek
10 New Member
Nope -- I didn't know about the -Wall flag. I will use it from now on... thanks for the tip!
Jul 15 '07 #11
JosAH
11,448 Recognized Expert MVP
Nope -- I didn't know about the -Wall flag. I will use it from now on... thanks for the tip!
Read gcc's manual page; it has all sorts of nifty little flags that can reduce the
code size, speed thing up etc. etc. I always use that -Wall flag despite the
fact that gcc whines about a lot of things then, and I mean *a lot*. It does
improve your code quality though if you manage to keep gcc's mouth shut ;-)

kind regards,

Jos
Jul 15 '07 #12
ElderGeek
10 New Member
Well, I'm all for anything that will improve the quality of my code, especially when learning a new language. I'm also going to drop you a private message... please watch for it.

Thanks!
Jul 15 '07 #13
JosAH
11,448 Recognized Expert MVP
Well, I'm all for anything that will improve the quality of my code, especially when learning a new language. I'm also going to drop you a private message... please watch for it.

Thanks!
I just replied.

kind regards,

Jos
Jul 15 '07 #14

Sign in to post your reply or Sign up for a free account.

Similar topics

58
10188
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of code... TCHAR myArray; DoStuff(myArray);
9
4815
by: justanotherguy63 | last post by:
Hi, I am designing an application where to preserve the hierachy and for code substitability, I need to pass an array of derived class object in place of an array of base class object. Since I am using vector class(STL), the compiler does not allow me to do this. I do realize there is a pitfall in this approach(size of arrays not matching etc), but I wonder how to get around this problem. I have a class hierachy with abstract base...
8
4119
by: kalinga1234 | last post by:
there is a problem regarding passing array of characters to another function(without using structures,pointer etc,).can anybody help me to solve the problem.
2
2503
by: beetle | last post by:
Hello, I'm storing data in several different binary tree's. The root node is located in a struct containing general data about the tree. struct lnode { char *fname; int nentry;
4
8439
by: dogalacar | last post by:
Hi All, I am trying to pass array of structures from a C dll to C# as msdn sample does(outarrayofstructs sample) but PtrToStructure function gives error : --> "structure must not be a value class" What i am doing wrong ? here is the code
2
4846
by: Steve Turner | last post by:
I have read several interesting posts on passing structures to C dlls, but none seem to cover the following case. The structure (as seen in C) is as follows: typedef struct tag_scanparm { short cmd; short fdc; WORD dsf; short boxcar; short average; short chan_ena;
2
5626
by: Subodh | last post by:
Hi All, I want to use a C++ API in a static lib that returns a linked List in C# I am planning to P/Invoke to perform the Interop, I would like to know which way will be better to interop a Linked list to C# in terms of performance, I have came across following methods I am creating a flat DLL to export the static lib functionality.
20
2187
by: jason | last post by:
Hello, I'm a beginning C programmer and I have a question regarding arrays and finding the number of entries present within an array. If I pass an array of structures to a function, then suddenly I can't use sizeof(array) / sizeof(array) anymore within that function ? Help - What point am I missing ?
7
2161
by: pereges | last post by:
which one do you think is better ? I need to make my program efficient and in some places I have passed the copy of a variable which makes life some what easy while writing huge expressions but they do requrie much more memory than a simple pointer.
0
9706
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10575
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10330
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10319
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10076
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7616
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5520
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3816
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.