Pointers to structure and array of structure.

E

Excluded_Middle

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;
- tmp is structure of type M
- list is similar to above mentioned list
- i is an integer index
 
A

Artie Gold

Excluded_Middle said:
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;
- 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
 
J

J. J. Farrell

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;
- 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.
 
E

Excluded_Middle

void main()
{
int i;
int *x, *y;
x = (int *) calloc(10, sizeof(int));
for(i=0;i<10;i++)
x = 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 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.
 
M

Martin Ambuhl

Excluded_Middle said:
void main()
{
int i;
int *x, *y;
x = (int *) calloc(10, sizeof(int));
for(i=0;i<10;i++)
x = 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 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;
/* 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);
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);
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) = 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top