delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/05/20/14:35:15

From: ao950 AT FreeNet DOT Carleton DOT CA (Paul Derbyshire)
Newsgroups: comp.os.msdos.djgpp
Subject: Re: pointer problems
Date: 20 May 1997 07:21:37 GMT
Organization: The National Capital FreeNet
Lines: 35
Message-ID: <5lrje1$rvj@freenet-news.carleton.ca>
References: <5lrc4v$gsp$1 AT grissom DOT powerup DOT com DOT au>
Reply-To: ao950 AT FreeNet DOT Carleton DOT CA (Paul Derbyshire)
NNTP-Posting-Host: freenet5.carleton.ca
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

David Orme (phreadd AT powerup DOT com DOT au) writes:
> This is slightly off topic but please help.
> 
> 
> I want to do something like this:
>   a->b[i].d[j] = &(a->c[k]);  /* the &() is wrong, I get a sigsev fault */
> 
> where "d" is an array of pointers
>   and "c" is an aray of integers
> 
> How do I make d[j] point to the address of b[k]?
> 
> Please don't recommend I abolish "a", although that does make the assignment 
> operation simpler (I've done it before, it's b[i].d[j] = &c[k]).
> 
> The nested properties of this structure are required for my program as it 
> makes the rest of the program simpler, though longer :-)

I suspect that i, j, or k are out of th array bounds. Note that if c[] was
declared as an int c[6] then valid k's are 0, 1, 2, 3, 4, and 5, but 6
will cause a segfault.
Also, if you have an array c by itself, sized say 10, and an array c
member of struct a, sized 6, then there are two different arrays and c[10]
is valid but a->c[10] is not.
Also, a must be a pointer to a struct, and if member c is a pointer and
not directly declared as a fixed size array, then you have to malloc the
space for your array and assign the returned pointer to a->c, checking for
NULL, BEFORE any reference to a->c[k].

--
    .*.  Where feelings are concerned, answers are rarely simple [GeneDeWeese]
 -()  <  When I go to the theater, I always go straight to the "bag and mix"
    `*'  bulk candy section...because variety is the spice of life... [me]
Paul Derbyshire ao950 AT freenet DOT carleton DOT ca, http://chat.carleton.ca/~pderbysh

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019