473,225 Members | 1,616 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,225 software developers and data experts.

declaring array of pointers

Hi,

I'd be grateful if someone could clarify this for me. In the linked list
structure my intention is to declare an array of length 3 containing
pointers to node
eg. Node *Iterators[3]
The compiler seems to interpret this as a pointer to an array of 3 nodes
instead. This interpretation ensures that the second assigment to mynode
below fails compilation with the given message.

Could someone explain to me how to correctly declare an array of length 3
containing pointers to node

cheers
#include <stdio.h>

typedef struct Node
{
void *Data;
struct Node *Next;
struct Node *Previous;
} Node;

/* structure to represent a linked list */
typedef struct LinkedList
{
Node *Head; /* start of linked list */
Node *Tail; /* end of linked list */
long NumberOfNodes;
short NumberOfIterators;
Node *Iterators[3];
} LinkedList;

LinkedList *mLinkedLists[2];
void main()
{
Node *mynode;
LinkedList *LL=NULL;

LL = mLinkedLists[0];

mynode = LL->Iterators[0]; <- this compiles

mynode = LL->Iterators; <- this doesn't : cannot convert from 'struct Node
*[3]' to 'struct Node *

printf("out\n");
}
Nov 14 '05 #1
8 2546
"Steve Lambert" <st***********@ntlworld.com> writes:
I'd be grateful if someone could clarify this for me. In the linked list
structure my intention is to declare an array of length 3 containing
pointers to node
eg. Node *Iterators[3]
That's the correct syntax.
The compiler seems to interpret this as a pointer to an array of 3 nodes
instead. This interpretation ensures that the second assigment to mynode
below fails compilation with the given message.
It's a misunderstanding of the error message, not a problem with
the compiler.
/* structure to represent a linked list */
typedef struct LinkedList
{ .... Node *Iterators[3];
} LinkedList;
....
Node *mynode;
LinkedList *LL=NULL;

LL = mLinkedLists[0];

mynode = LL->Iterators[0]; <- this compiles
mynode is type Node *.
LL->Iterators[0] is type Node *.
Fine.
mynode = LL->Iterators; <- this doesn't : cannot convert from 'struct Node
*[3]' to 'struct Node *


mynode is type Node *.
LL->Iterators is type Node *[3], just as you declared it.
Problem: you can't assign a Node *[3] to a Node *.

The compiler's error message is slightly deceptive, because the
name of an array object is usually converted into a pointer to
the first element of the array. In this case, that means that
the actual type of the value being assigned is Node **. If you
defined mynode to be of type Node **, the second assignment would
be fine (although the first assignment would become invalid).

What are you trying to do with that statement?
--
"Give me a couple of years and a large research grant,
and I'll give you a receipt." --Richard Heathfield
Nov 14 '05 #2
Steve Lambert wrote:
Node *mynode;
mynode = LL->Iterators[0]; <- this compiles
mynode = LL->Iterators; <- this doesn't : cannot convert from 'struct Node
*[3]' to 'struct Node *


Your essential problem here is that "mynode" is a Node*, and
LL->Iterators is an array of Node*. These types are naturally
incompatible, just as you can't assign an array of int into an int (an
array is not an integer.)

On the other hand, you *can* assign an array of int to an int*. This is
because when used in expression contexts, the name of an array "decays"
into a pointer to its first element implicitly. In your case,
LL->Iterators would "decay" into &LL->Iterators[0], which has type
Node** (which is still incompatible with Node* mynode).
--
Derrick Coetzee
I grant this newsgroup posting into the public domain. I disclaim all
express or implied warranty and all liability. I am not a professional.
Nov 14 '05 #3
"Steve Lambert" <st***********@ntlworld.com> wrote in message news:<FM***************@newsfe5-win.ntli.net>...
... In the linked list
structure my intention is to declare an array of length 3 containing
pointers to node
eg. Node *Iterators[3]
You did. "Size 3" is more idiomatic than "length 3" I think.

<snip> mynode = LL->Iterators[0]; <- this compiles "//" and "/* */" are syntax for comments
-- use them even for meta-comments!
mynode = LL->Iterators; <- this doesn't


Whatever and Whatever[0] are always of different types
(the former often a pointer to the latter) -- why would
you expect *both* statements to compile?

James
Nov 14 '05 #4


Steve Lambert wrote:
Hi,

I'd be grateful if someone could clarify this for me. In the linked list
structure my intention is to declare an array of length 3 containing
pointers to node
eg. Node *Iterators[3]
The compiler seems to interpret this as a pointer to an array of 3 nodes
instead. This interpretation ensures that the second assigment to mynode
below fails compilation with the given message.

Could someone explain to me how to correctly declare an array of length 3
containing pointers to node

cheers
#include <stdio.h>

typedef struct Node
{
void *Data;
struct Node *Next;
struct Node *Previous;
} Node;

/* structure to represent a linked list */
typedef struct LinkedList
{
Node *Head; /* start of linked list */
Node *Tail; /* end of linked list */
long NumberOfNodes;
short NumberOfIterators;
Node *Iterators[3];
} LinkedList;

LinkedList *mLinkedLists[2];
void main()
{
Node *mynode;
LinkedList *LL=NULL;

LL = mLinkedLists[0];

mynode = LL->Iterators[0]; <- this compiles


It may compile but it is very flawed and will not run.
LL has the value NULL and does not point to storage.
Since you are trying to access storage that is not, the
execution will fail.

You can type the member Iterators as
Node *Iterators[3];
making Iterators point to an array of 3 Node pointers.
Make a LinkedList object
LinkledList mylist;
Then you can access or assign Iterators with code something
like this:
mylist.Iterators[0] = malloc(sizeof Node);
or
Node newnode;
mylist.Iterators[0] = &newnode;
and
mylist.Iterators[0]->Data = /* whatever */

One the other hand out might make the member Iterators:
Node (*Iterators)[3];
Iterators will point to an array of 3 Node objects (not pointers).
Here is an example, in function main below:

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

typedef struct Node
{
int Data;
struct Node *Next;
struct Node *Previous;
} Node;

/* structure to represent a linked list */
typedef struct LinkedList
{
Node *Head; /* start of linked list */
Node *Tail; /* end of linked list */
size_t NumberOfNodes;
size_t NumberOfIterators;
Node (*Iterators)[3];
} LinkedList;
/* Prototypes */
int AddNode(LinkedList *p, int data, size_t IterNo);
void FreeLinkedList(LinkedList *p);

int main(void)
{
LinkedList myList = {NULL};
size_t i,j;

AddNode(&myList, 44,0);
AddNode(&myList,33,2);
printf("myList.NumberOfIterators = %u\n",
myList.NumberOfIterators);
for(i = 0; i < myList.NumberOfIterators;i++)
for(j = 0; j < 3; j++)
{
myList.Iterators[i][j].Data = i+j;
printf("myList.Iterator[%u][%u].Data = %d\n",
i,j, myList.Iterators[i][j].Data);
}
printf("\nmyList.Head->Data = %d\n",myList.Head->Data);
FreeLinkedList(&myList);
return 0;
}

int AddNode(LinkedList *p, int data, size_t IterNo)
{
Node *new;
Node (*itmp)[3];

if((new = malloc(sizeof *new)) == NULL) return 0;
itmp = realloc(p->Iterators,(sizeof *itmp)*IterNo);
if(IterNo && itmp == NULL)
{
free(new);
return 0;
}
p->Iterators = itmp;
p->NumberOfIterators = IterNo;
new->Data = data;
if(p->NumberOfNodes == 0)
{
new->Previous = new->Next = NULL;
p->Head = p->Tail = new;
}
else
{
new->Previous = NULL;
new->Next = p->Head;
p->Head->Previous = new;
p->Head = new;
}
p->NumberOfNodes++;
return 1;
}

void FreeLinkedList(LinkedList *p)
{
Node *tmp;

for(tmp = p->Head; tmp;p->Head = tmp)
{
tmp = p->Head->Next;
free(p->Head);
}
free(p->Iterators);
p->Head = p->Tail = NULL;
p->Iterators = NULL;
p->NumberOfIterators = p->NumberOfNodes = 0;
return;
}

--
Al Bowers
Tampa, Fl USA
mailto: xa******@myrapidsys.com (remove the x to send email)
http://www.geocities.com/abowers822/

Nov 14 '05 #5
James Dow Allen wrote:

<snip>
mynode = LL->Iterators[0]; <- this compiles

"//" and "/* */" are syntax for comments
-- use them even for meta-comments!


Don't use // in newsgroups, nor for code expected to compiler
under C90.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #6
James Dow Allen wrote:

"Steve Lambert" <st***********@ntlworld.com> wrote in message news:<FM***************@newsfe5-win.ntli.net>...
... In the linked list
structure my intention is to declare an array of length 3 containing
pointers to node
eg. Node *Iterators[3]


You did. "Size 3" is more idiomatic than "length 3" I think.


I agree.
Size is a dimension of objects.
Length is a dimension of strings.

--
pete
Nov 14 '05 #7
Derrick Coetzee <dc****@moonflare.com> writes:
On the other hand, you *can* assign an array of int to an int*. This is
because when used in expression contexts, the name of an array "decays"
into a pointer to its first element implicitly. In your case,
LL->Iterators would "decay" into &LL->Iterators[0], which has type
Node** (which is still incompatible with Node* mynode).


I'm curious to know if anyone can shed some light as to the origin of
the phrase "decays into a pointer" when talking about the behavior
of array names used in C. It isn't in K&R (at least not that I
recall), and I don't remember seeing it in any of the other C books
I've read. Personally I find the term misleading, and suspect it
probably confuses C novices more than it helps them. Anyone have
any personal experiences, either pro or con, to report on that?

Nov 14 '05 #8


Tim Rentsch wrote:
I'm curious to know if anyone can shed some light as to the origin of
the phrase "decays into a pointer" when talking about the behavior
of array names used in C.

?
My first experience with the phrase is in the comp.lang.c faq.
Question 6.3 located at
http://www.eskimo.com/~scs/C-faq/q6.3.html

the faq guestion cites the three exceptions.

--
Al Bowers
Tampa, Fl USA
mailto: xa******@myrapidsys.com (remove the x to send email)
http://www.geocities.com/abowers822/

Nov 14 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

29
by: Friday | last post by:
Sorry if this is the wrong group. I tried to find the one I thought would be most relevant. I'm an old PHP guy, who knows little about asp and NOTHING about asp.net, but need to learn at least...
14
by: Eric Bantock | last post by:
Very basic question I'm afraid. Once an array has been declared, is there a less tedious way of assigning values to its members than the following: myarray=8; myarray=3; myarray=4; myarray=0;...
12
by: junky_fellow | last post by:
How can I declare a function that returns a pointer to one dimensional array ?
1
by: Bern McCarty | last post by:
I am using MEC++ in VC 7.1. I had a method on a __gc object that looked like this: __property System::UInt32 get_MyProperty(void) { System::Byte __pin * pinBytes = &m_byteArray; // entire...
3
by: mark | last post by:
When I declare an array as double(,) then try to use it I get an error: "Object reference not set to an instance of an object." I have found that I can redim the array and all is well. Is my...
12
by: gcary | last post by:
I am having trouble figuring out how to declare a pointer to an array of structures and initializing the pointer with a value. I've looked at older posts in this group, and tried a solution that...
11
by: rayreeves | last post by:
How do I declare an array of references to the elements of an array of values? Ray
15
by: edson | last post by:
Greetings. My microcontroller program uses arrays of strings. I want to store the strings AND their pointers in ROM. I am able to initialize the arrays so that the strings are put in ROM, but the...
8
by: bintom | last post by:
What are the differences between the following methods of declaring strings? char string1 = "C++ forum"; char* string2 = "C++ forum"; I know that the first uses the array notation, whereas...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.