C pointer/array duality confusion


 
Thread Tools Search this Thread
Top Forums Programming C pointer/array duality confusion
# 1  
Old 06-07-2010
C pointer/array duality confusion

Hi all,

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

Code:
/**
* An array of ptrs to sections
*/
typedef section_t* region_t[SECTION_LENGTH];

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

Code:
typedef struct container
{
    region_t regions[N_THREADS];
}container_t;

And I have a static instance of the container type...

Code:
static container_t ctn;

In the same file as the ctn variable. I have a function which needs to access these region arrays...

Code:
region_t region = ctn.regions[nth];

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

Code:
  region[i]->section_t_data_member = x;

Any help much appreciated. As I say, couldn't find an example which clarifies this.
# 2  
Old 06-07-2010
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..
# 3  
Old 06-07-2010
Thanx for your reply,

"you're trying to initialize a struct, from a pointer."

I thought with the typedef...

Code:
typedef section_t* region_t[SECTION_LENGTH];

This would mean that the following statement would be assigning a ptr to a ptr...

Code:
region_t region = ctn.regions[nth];

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...
Code:
section_t** region = ctn.regions[nth];

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

Code:
  region[i]->section_t_data_member = x;

# 4  
Old 06-07-2010
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.
# 5  
Old 06-07-2010
Quote:
Originally Posted by Corona688
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Pointer for 2D array seems to be 3D in C

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)
Discussion started by: yifangt
1 Replies

2. Programming

Character pointer to Character array

how to copy content of character pointer to character array in c programming.. char *num; char name=num; (1 Reply)
Discussion started by: zinat
1 Replies

3. UNIX for Dummies Questions & Answers

File System and Storage Array Confusion

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)
Discussion started by: munna529
6 Replies

4. Programming

Pointer confusion

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)
Discussion started by: dragonpoint
1 Replies

5. Programming

Unclear pointer and array

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)
Discussion started by: yifangt
29 Replies

6. Programming

structure pointer array as function parameters

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)
Discussion started by: mscoder
2 Replies

7. Programming

help with char pointer array in C

i have an array like #define NUM 8 .... new_socket_fd = accept(socket_fd, (struct sockaddr *) &cli_addr, &client_length); char *items = {"one", "two", "three", "four", "five", "six", "seven", "eight"}; char *item_name_length = {"3", "3", "5", "4", "4", "3", "5", "5"}; ... (1 Reply)
Discussion started by: omega666
1 Replies

8. Programming

pass a pointer-to-pointer, or return a pointer?

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)
Discussion started by: aaronwong
11 Replies

9. UNIX for Dummies Questions & Answers

Storing pointer array in C

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)
Discussion started by: arunkumar_mca
1 Replies

10. UNIX for Dummies Questions & Answers

confusion (file pointer and file descripter)

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)
Discussion started by: johnray31
1 Replies
Login or Register to Ask a Question