Can anyone provide help with getting the right syntax regarding array/pointers in C in the following code? Can't locate a specific example which clarifies this...
Say I declare a typedef to an array of pointers to some type...
So now a "region_t" is an array of pointers to "section_t", "SECTION_LENGTH" long.
And now I declare a struct which contains an array of these...
And I have a static instance of the container type...
In the same file as the ctn variable. I have a function which needs to access these region arrays...
But the compiler complains of an "invalid initializer". But this array would have been initialised being a member of a static variable. I don't want to initialise it, just access it. Can anyone tell me where I'm going wrong here and how, if possible to declare a variable that access the elements of "region" like a regular array variable, i.e...
Any help much appreciated. As I say, couldn't find an example which clarifies this.
Well, it is an invalid initializer -- you're trying to initialize a struct, from a pointer. You've got an array of pointers, so array[n] gives you a pointer, not a struct. To convert a pointer to a not-pointer you use the * operator.
The duality comes from the fact that a pointer and an array are both the same thing at the machine level: an address. The only difference is the number of elements(one, or many).
Last edited by Corona688; 06-07-2010 at 01:53 PM..
"you're trying to initialize a struct, from a pointer."
I thought with the typedef...
This would mean that the following statement would be assigning a ptr to a ptr...
Because a region_t is a ptr type...or in other words, an array of section_t* ptrs. Hence my confusion, I thought in C an array and a ptr were essentially the same things, so this would work.
Anyway I got it to work by changing the assignment to the following...
I realised that "ctn.regions[nth]" is basically an array without the subscript, which means it's a ptr. And what it points to is a ptr to a "section_t"... voila a ptr to a ptr.
And I can access the section_t* by using the subscript, i.e...
You're on a one-way track to segfault land if you don't understand what kind of pointers you're using. It's not enough that it compiles. Have you even allocated memory for your pointer to pointer pointers?
If I knew what you were actually trying to do I could show a way to do it.
You're on a one-way track to segfault land if you don't understand what kind of pointers you're using. It's not enough that it compiles. Have you even allocated memory for your pointer to pointer pointers?
I have allocated the memory. I've ran and tested the program and it works without seg-faulting. I'm "pretty" certain that I understand what's going on now with the pointers, although of course I might be wrong.
Quote:
If I knew what you were actually trying to do I could show a way to do it.
Thanks. I'm building a type of thread-pool, it's a pretty complex design so I didn't want to burden people with the details, but basically the code I provided is initialising the thread-pool structures which hold "task" information. Suffice to say it's currently working, but I'll watch out out seg-faults, maybe run it through valgrinde.
I am struggling with the pointer to 2D-array (cf: 2D array of pointers). Can anybody help me elaborate how the pointer x moves in the memory to access the individual of y, especially the high lighted lines?
I have talked to one of the curators of the forum, but I am still not quite clear.
Here... (1 Reply)
Hi Friends,
I have a host(Suse Linux 10.4) which has 2 luns presented from 2 different arrays HP eva and xp. we are planning to migrate hp eva to 3par. When i look for physical volume i see /dev/dm-4, /dev/dm-5, /dev/dm-7and when i look for multipath -ll i see dm-8,dm-9,dm-7. So i can't confirm... (6 Replies)
Here are two programs that pass a pointer to a variable but behave differently. Shouldnt the i in second program be 0 after the function call?
#include<stdio.h>
void changeI(int *i)
{
*i = 10;
}
int main(void)
{
int i=5;
printf("%d before\n", i);
changeI(&i);
printf("%d... (1 Reply)
Hello,
The purpose of the program is to print a sub string from the prompt inputs. I do not understand why char pointer does not work but char array will for line 40 and Line 41.
./a.out thisisatest 0 8
substring = "thisisat"And my code is:
#include <stdio.h>
#include <stdlib.h>
#include... (29 Replies)
if i create an array of pointers to a structure "struct node" as:
struct node *r;
and create "n" number of "linked lists" and assign it to the various struct pointers r using some function with a return type as structure pointer as:
r=multiplty(.......) /*some parameters*/
is... (2 Replies)
If one wants to get a start address of a array or a string or a block of memory via a function, there are at least two methods to achieve it:
(1) one is to pass a pointer-to-pointer parameter, like:
int my_malloc(int size, char **pmem)
{
*pmem=(char *)malloc(size);
if(*pmem==NULL)... (11 Replies)
All ..
I am having a pointer array . And trying to store the addess into that pointer array . please see below the problem i faced
code:
int cnt1;
char *t_array;
char *f_array;
for(cnt1=0; cnt1<1000; cnt1++)
{
t_array =... (1 Reply)
Hi everybody,
i am newbie to unix and confused with file pointers and file descripters.
could anyone help me to clear my doubts ..
when we call unix system calls to create a file then we are dealing wih file descripters
i think file descripters are also normals file as stored inhard disks... (1 Reply)