Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Pointers to structure and array of structure.

Reply
Thread Tools

Pointers to structure and array of structure.

 
 
Excluded_Middle
Guest
Posts: n/a
 
      10-24-2004
how to convert these two notation in pointer form:

1 - list[count] = value;
(explaination)
- list is an array of structure of type M.
- count is integer index
- value is a structure of type M.

2 - tmp = list[i];
- tmp is structure of type M
- list is similar to above mentioned list
- i is an integer index
 
Reply With Quote
 
 
 
 
Artie Gold
Guest
Posts: n/a
 
      10-24-2004
Excluded_Middle wrote:
> how to convert these two notation in pointer form:
>
> 1 - list[count] = value;
> (explaination)
> - list is an array of structure of type M.
> - count is integer index
> - value is a structure of type M.
>
> 2 - tmp = list[i];
> - tmp is structure of type M
> - list is similar to above mentioned list
> - i is an integer index


<rant>
[sniff, sniff]Yup, smells like homework.

DYOFHW!!!!!
</rant>

Find the FAQ. Read the FAQ. Show us what you have so far. *Then* we'll help.

HTH,
--ag
--
Artie Gold -- Austin, Texas

"If you don't think it matters, you're not paying attention."
 
Reply With Quote
 
 
 
 
J. J. Farrell
Guest
Posts: n/a
 
      10-25-2004
http://www.velocityreviews.com/forums/(E-Mail Removed) (Excluded_Middle) wrote in message news:<(E-Mail Removed). com>...
> how to convert these two notation in pointer form:
>
> 1 - list[count] = value;
> (explaination)
> - list is an array of structure of type M.
> - count is integer index
> - value is a structure of type M.
>
> 2 - tmp = list[i];
> - tmp is structure of type M
> - list is similar to above mentioned list
> - i is an integer index


Use pointer notation. If you tell us what problems you have with
your solution, we should be able to help.
 
Reply With Quote
 
Excluded_Middle
Guest
Posts: n/a
 
      10-26-2004
void main()
{
int i;
int *x, *y;
x = (int *) calloc(10, sizeof(int));
for(i=0;i<10;i++)
x[i] = i;
}

in the above program I want to to access x by pointer notation instead
of array notation i.e. instead of using

x[i] = i;

I use

*(x + i) = i;
it doesn't work. secondly I use

*x = i;
x++;

work fine but then I lost the pointer to first element i.e. x[0].

I am giving this example using simple int but I really need to access
a structure with pointer notation. which give me segmentation faults
or other values.

/********** Guys like "ARTIE GOLD" should read below
*****************/
This is part of an assignment question and I m not asking you guys to
solve it all I ask is little help for calloc and structure array. Guys
can't answer my question should not give me suggestion of searching
for FAQ because I am doing my best to find the answer.
/***************************/

Thank you to all of you except "ARTIE GOLD" kinda guys.
 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      10-26-2004
Excluded_Middle wrote:

> void main()
> {
> int i;
> int *x, *y;
> x = (int *) calloc(10, sizeof(int));
> for(i=0;i<10;i++)
> x[i] = i;
> }
>


This program is badly broken. See below.

> in the above program I want to to access x by pointer notation instead
> of array notation i.e. instead of using
>
> x[i] = i;
>
> I use
>
> *(x + i) = i;
> it doesn't work. secondly I use


What in the world do you mean by doesn't work? Of course it works:

#include <stdlib.h> /* mha: added; needed for OP's code */
#include <stdio.h> /* mha: not needed for the OP's code,
used to display the results of the
two approaches */


int /* mha: was the Schildty 'void' */ main()
{
int i;
int *x, *y;
/* mha: replaced wasteful calloc call, which also sillily cast the
return value from calloc */
if (!(x = malloc(10 * sizeof *x))) {
fprintf(stderr, " x allocation failed.\nQuiting");
exit(EXIT_FAILURE);
}
for (i = 0; i < 10; i++)
x[i] = i;
/* mha: the following is added to show the results of the assignments
both from the OP's code and from pointer-based assignments */
for (i = 0; i < 10; i++)
printf("x[%d] = %d\n", i, x[i]);
printf("\n");
/* mha: parallel to the above, but in using y, and running through
the arrays backward */
if (!(y = malloc(10 * sizeof *y))) {
fprintf(stderr, "allocation failed.\nQuiting");
free(x);
exit(EXIT_FAILURE);
}
for (i = 9; i >= 0; i--)
*(y + i) = i;
for (i = 9; i >= 0; i--)
printf("*(y+%d) = %d\n", i, y[i]);
printf("\n");
free(y);

free(x); /* mha: added, needed in OP's code */
return 0; /* mha: added, needed in OP's code
unless he has a C99 compiler */
}

[output]
x[0] = 0
x[1] = 1
x[2] = 2
x[3] = 3
x[4] = 4
x[5] = 5
x[6] = 6
x[7] = 7
x[8] = 8
x[9] = 9

*(y+9) = 9
*(y+ = 8
*(y+7) = 7
*(y+6) = 6
*(y+5) = 5
*(y+4) = 4
*(y+3) = 3
*(y+2) = 2
*(y+1) = 1
*(y+0) = 0


 
Reply With Quote
 
 
 
Reply

Thread Tools

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

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
pointers, pointers, pointers... cerr C Programming 12 04-07-2011 11:17 PM
pointers and array of pointers Piotrek C Programming 8 04-06-2007 08:02 AM
Pointers, linked list, array of pointers Sean C++ 2 09-24-2006 01:35 PM
array of pointers to a structure jof C Programming 1 08-22-2004 10:22 AM
Pointers and References (and References to Pointers) Roger Leigh C++ 8 11-17-2003 10:14 AM



Advertisments