Go Back   Velocity Reviews > Newsgroups > C Programming
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

C Programming - Passing an array of struct to function

 
Thread Tools Search this Thread
Old 11-18-2008, 08:06 AM   #1
Default Passing an array of struct to function


I am passing an array of struct to a function to print its value. First
I am getting Segfaults and weired values. 2nd, is there any elegant way to
do this ?



/* Learning how to use an array of struct */


#include <stdio.h>
#include <stdlib.h>

enum { ARR_SIZE = 1 };

struct two_elem { char ch; char* word; };


void print_twoelem( struct two_elem*);


int main(void)
{

struct two_elem arr[ARR_SIZE];

char arr1[] = "ARNULD";
char arr2[] = "UTTRE";

struct two_elem s1;
struct two_elem s2;

s1.ch = 'a';
s1.word = arr1;
s2.ch = 'b';
s2.word = arr2;

arr[1] = s1;
arr[2] = s2;

/* this is fine as we are passing a point to the first element which is struct
two_ele and this is what exactly rquired by the function
*/
print_twoelem( arr );


return 0;
}



void print_twoelem( struct two_elem* p )
{
printf("first element = %c, || %s\n", p->ch, p->word);
++p;
printf("second element = %c, || %s\n", p->ch, p->word);
}

===================== OUTPUT ==============================
[arnuld@dune C]$ gcc4 -ansi -pedantic -Wall -Wextra array-of-struct.c
[arnuld@dune C]$ ./a.out
first element = n, || <-
second element = a, || ARNULD
Segmentation fault
[arnuld@dune C]$



--
www.lispmachine.wordpress.com
my email is @ the above blog.




arnuld
  Reply With Quote
Old 11-18-2008, 08:22 AM   #2
Nate Eldredge
 
Posts: n/a
Default Re: Passing an array of struct to function
arnuld <> writes:

> I am passing an array of struct to a function to print its value. First
> I am getting Segfaults and weired values. 2nd, is there any elegant way to
> do this ?
>
>
>
> /* Learning how to use an array of struct */
>
>
> #include <stdio.h>
> #include <stdlib.h>
>
> enum { ARR_SIZE = 1 };
>
> struct two_elem { char ch; char* word; };
>
>
> void print_twoelem( struct two_elem*);
>
>
> int main(void)
> {
>
> struct two_elem arr[ARR_SIZE];


ARR_SIZE is 1. I think you want it to be 2.

>
> char arr1[] = "ARNULD";
> char arr2[] = "UTTRE";
>
> struct two_elem s1;
> struct two_elem s2;
>
> s1.ch = 'a';
> s1.word = arr1;
> s2.ch = 'b';
> s2.word = arr2;
>
> arr[1] = s1;
> arr[2] = s2;


Remember C arrays are indexed from zero. So you want to assign to
arr[0] and arr[1].

You could also do this more concisely with something like

struct two_elem arr[2] = { { 'a', "ARNULD" }, { 'b', "UTTRE" } };

The only difference being that in my version you cannot overwrite the
strings "ARNULD" and "UTTRE" (though you could set the pointers
arr[0].word and arr[1].word to point to some different strings). But
your program doesn't do that anyway.

> /* this is fine as we are passing a point to the first element which is struct
> two_ele and this is what exactly rquired by the function
> */


You might find it more helpful to think that the array decays to a
pointer to its *zeroth* element. I.e. passing `arr' to the function is
the same as passing `&(arr[0])'.

> print_twoelem( arr );
>
>
> return 0;
> }
>
>
>
> void print_twoelem( struct two_elem* p )
> {
> printf("first element = %c, || %s\n", p->ch, p->word);
> ++p;
> printf("second element = %c, || %s\n", p->ch, p->word);
> }
>
> ===================== OUTPUT ==============================
> [arnuld@dune C]$ gcc4 -ansi -pedantic -Wall -Wextra array-of-struct.c
> [arnuld@dune C]$ ./a.out
> first element = n, || <-
> second element = a, || ARNULD
> Segmentation fault
> [arnuld@dune C]$



Nate Eldredge
  Reply With Quote
Old 11-18-2008, 08:29 AM   #3
Nick Keighley
 
Posts: n/a
Default Re: Passing an array of struct to function
On 18 Nov, 08:06, arnuld <sunr...@invalid.address> wrote:

> I am passing an array of struct to a function to print its value. First
> I am getting Segfaults and weired values. 2nd, is there any elegant way to
> do this ?
>
> /* Learning how to use an array of struct */
>
> #include <stdio.h>
> #include <stdlib.h>
>
> enum { ARR_SIZE = 1 };


that's an unusual size for an array



> struct two_elem { char ch; char* word; };
>
> void print_twoelem( struct two_elem*);
>
> int main(void)
> {
>
> * struct two_elem arr[ARR_SIZE];
>
> * char arr1[] = "ARNULD";
> * char arr2[] = "UTTRE";
>
> * struct two_elem s1;
> * struct two_elem s2;
>
> * s1.ch = 'a';
> * s1.word = arr1;
> * s2.ch = 'b';
> * s2.word = arr2;
>
> * arr[1] = s1;


oops. You accessed the second element of a single element array.

> * arr[2] = s2;


oops. You accessed the third element of a single element array.
C arrays start from zero

> * /* this is fine as we are passing a point to the first element which is struct
> * * *two_ele and this is what exactly rquired by the function
> * */


yes

> * print_twoelem( arr );
>
> * return 0;
>
> }
>
> void print_twoelem( struct two_elem* p )
> {
> * printf("first element *= %c, || %s\n", p->ch, p->word);


you access the uninitialised 1st element

> * ++p;
> * printf("second element = %c, || %s\n", p->ch, p->word);


you access the non-existent 2nd element


>
> }
>
> ===================== OUTPUT ==============================
> [arnuld@dune C]$ gcc4 -ansi -pedantic -Wall -Wextra array-of-struct.c
> [arnuld@dune C]$ ./a.out
> first element *= n, || <-
> second element = a, || ARNULD
> Segmentation fault


quite

this is how I'd write it (some things are just different personnel
style)

************************************************** *************

#include <stdio.h>
#include <stdlib.h>

#define ARR_SIZE 2

#define ARRAY_SIZE(A) sizeof(A)/sizeof(*A)

typedef struct
{
char ch;
char* word;
} Two_elem;

void print_twoelem (const Two_elem p[], int n)
{
int i;

for (i = 0; i < n; i++)
printf ("first element = %c, || %s\n", p[i].ch, p[i].word);
}

int main (void)
{
Two_elem arr [ARR_SIZE];
char arr1[] = "ARNULD";
char arr2[] = "UTTRE";
Two_elem s1;
Two_elem s2;

s1.ch = 'a';
s1.word = arr1;
s2.ch = 'b';
s2.word = arr2;

arr [0] = s1;
arr [1] = s2;

print_twoelem (arr, ARRAY_SIZE (arr));

return 0;
}

--
Nick Keighley

"That's right!" shouted Vroomfondel "we demand rigidly defined
areas of doubt and uncertainty!"


Nick Keighley
  Reply With Quote
Old 11-18-2008, 08:31 AM   #4
Nick Keighley
 
Posts: n/a
Default Re: Passing an array of struct to function
or

#include <stdio.h>
#include <stdlib.h>

#define ARRAY_SIZE(A) sizeof(A)/sizeof(*A)

typedef struct
{
char ch;
char* word;
} Two_elem;

void print_twoelem (const Two_elem p[], int n)
{
int i;

for (i = 0; i < n; i++)
printf ("first element = %c, || %s\n", p[i].ch, p[i].word);
}

int main (void)
{
Two_elem arr [] = {{'a', "ARNULD"}, {'b', "UTTRE"}};

print_twoelem (arr, ARRAY_SIZE (arr));

return 0;
}



Nick Keighley
  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
VHDL and EDK: Custom IP core containing an array as a port using EDK allsey_1987 Hardware 0 10-27-2009 02:26 PM
constants as of array of integers, for loops octavsly Hardware 0 04-25-2009 11:53 AM
equivalent function for itoa in Linux gcc compiler suse Software 0 03-06-2009 05:30 AM
How to assign a returns value of a javascript function to a hiddenfield in a webpart Chander Software 0 12-20-2007 09:14 AM
Re: Passing %? Anthony & Virginia A+ Certification 0 02-26-2004 01:14 AM




SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46