473,725 Members | 2,251 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Question : 3d array with pointer to pointer.

I was trying to define 3d array with pointer to pointer. I wrote like
following.

int ***d;
nx = 3;
ny = 5;
nz = 4;

d = (int ***)malloc((int ) nx*sizeof(int **));
*d = (int **)malloc((int) nx*ny*sizeof(in t*));
**d = (int *)malloc((int) nx*ny*nz*sizeof (int));
for(i = 1; i < nx ; i++) d[i] = d[i-1] + ny;
for(i = 0; i < nx ; i++)
for(j = 1; j < ny ; j++) d[i][j] = d[i][j-1] + nz;
for(i=0 ; i < nx; i ++)
for(j = 0; j < ny; j++)
for(k = 0 ; k < nz; k++)
d[i][j][k] = 100*(i+1) + 10*(j+1) + k;
it compiled ok. but when I ran it, it shows error message like
"segementat ion fault.."

What did I do wrong ? can someone help me ?

thank you.

Nov 12 '06 #1
4 2818
Joonshik Kim said:
I was trying to define 3d array with pointer to pointer. I wrote like
following.

int ***d;
nx = 3;
ny = 5;
nz = 4;
d = malloc(nx * sizeof *d);
if(d == NULL) { HANDLE_THE_ERRO R; }

for(i = 0; i < nx; i++)
{
d[i] = malloc(ny * sizeof *d[i]);
if(d[i] == NULL) { CLEAN_UP; HANDLE_THE_ERRO R; }

for(j = 0; j < ny; j++)
{
d[i][j] = malloc(nz * sizeof *d[i][j]);
if(d[i][j] == NULL) { CLEAN_UP; HANDLE_THE_ERRO R; }
for(k = 0; k < nz; k++)
{
d[i][j][k] = 42;
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: normal service will be restored as soon as possible. Please do not
adjust your email clients.
Nov 12 '06 #2
Oh..Thank you..
It works nice..

"Richard Heathfield" <in*****@invali d.invalidwrote in message
news:pq******** *************** *******@bt.com. ..
Joonshik Kim said:
I was trying to define 3d array with pointer to pointer. I wrote like
following.

int ***d;
nx = 3;
ny = 5;
nz = 4;

d = malloc(nx * sizeof *d);
if(d == NULL) { HANDLE_THE_ERRO R; }

for(i = 0; i < nx; i++)
{
d[i] = malloc(ny * sizeof *d[i]);
if(d[i] == NULL) { CLEAN_UP; HANDLE_THE_ERRO R; }

for(j = 0; j < ny; j++)
{
d[i][j] = malloc(nz * sizeof *d[i][j]);
if(d[i][j] == NULL) { CLEAN_UP; HANDLE_THE_ERRO R; }
for(k = 0; k < nz; k++)
{
d[i][j][k] = 42;
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: normal service will be restored as soon as possible. Please do not
adjust your email clients.

Nov 12 '06 #3
Richard Heathfield wrote:
[snip]
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: normal service will be restored as soon as possible. Please do not
adjust your email clients.
Hi Richard, it's Sarah Thompson here. I've been trying to contact you
urgently, but haven't succeeded through usual channels, so many
apologies for posting here but I am not aware of any alternative.

You need to contact your brother Robert urgently in connection with a
family matter. Feel free to email me directly and I'll fill you in on
the details.

Best regards,
Sarah (thompson a t email dt arc dt nasa d t gov)

Nov 15 '06 #4

Joonshik Kim wrote:
I was trying to define 3d array with pointer to pointer. I wrote like
following.

int ***d;
nx = 3;
ny = 5;
nz = 4;

d = (int ***)malloc((int ) nx*sizeof(int **));
You store one address into d.
*d = (int **)malloc((int) nx*ny*sizeof(in t*));
You store one address at *d.
**d = (int *)malloc((int) nx*ny*nz*sizeof (int));
You store one address at **d.
for(i = 1; i < nx ; i++) d[i] = d[i-1] + ny;
for(i = 0; i < nx ; i++)
for(j = 1; j < ny ; j++) d[i][j] = d[i][j-1] + nz;
You now try to access 60 addresses. What is wrong with this picture?

Nov 15 '06 #5

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

Similar topics

5
3184
by: overbored | last post by:
I can do this: int asdf; int* zxcv = asdf; but not this: int asdf; int** zxcv = asdf;
5
5641
by: pandapower | last post by:
Hi, I know about the equivalence of pointer and arrays.But my doubt comes when its for multidimentional arrays.I have read the C faq but still have some doubts. Suppose I have a declaration as 1. char array or char array and it will get decayed to 2. char *array
10
4117
by: Kieran Simkin | last post by:
Hi, I wonder if anyone can help me, I've been headscratching for a few hours over this. Basically, I've defined a struct called cache_object: struct cache_object { char hostname; char ipaddr; };
35
2729
by: David Cleaver | last post by:
Hello all, I was wondering if there were some sort of limitations on the "if" statement? I'm writing a program which needs to check a bunch of conditions all at the same time (basically). And I'm pretty sure the rest of the program is working just fine. The only thing I could think might be wrong is that the if statement can only hold so many values in itself? Let me show what I'm doing: if (table001]>>5]&b&0x1f] != 0 &&
28
2399
by: Wonder | last post by:
Hello, I'm confused by the pointer definition such as int *(p); It seems if the parenthesis close p, it defines only 3 integers. The star is just useless. It can be showed by my program: int main() {
24
3451
by: Michael | last post by:
Hi, I am trying to pass a function an array of strings, but I am having trouble getting the indexing to index the strings rather than the individual characters of one of the strings. I have declared an array as: char *stringArray = {"one","two","three","a"}; When I pass the array using:
14
1922
by: John Gerrard | last post by:
Hi, my name is John and this is my first posting to comp.lang.c. I am learning C. I am a high school student with interest in programming. I have a copy of "The C programming language second edition by Brian W Kernighan and Dennis M Ritchie". I am using gcc version 4.1 on SuSE 9. Here is my simple question. I want to set a matrix to contain some values in my program. I want to pass in the matrix array by address in a function. The...
18
2054
by: mdh | last post by:
>From p112 ( K&R). Given an array declared as static char arr= { { 0,1,........},{0,1,.....}}; let arr be passed as an argument to f. f( int (*arr) ) {....} It is noted that the parentheses are necessary else it would be
17
2389
by: DiAvOl | last post by:
Hello everyone, merry christmas! I have some questions about the following program: arrtest.c ------------ #include <stdio.h> int main(int agc, char *argv) {
8
1623
by: =?ISO-8859-1?Q?Konrad_M=FChler?= | last post by:
Hi, I've a list of objects. I iterate the list and read the value of each object for many operation like: x = myList.value1 + myList.value2 etc. My question: Is it efficient to always use myList or should I get the pointer to
0
8889
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8752
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9401
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9116
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8099
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6011
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4784
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3228
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2157
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.