From owner-freebsd-questions@freebsd.org Wed Apr 5 16:22:19 2017 Return-Path: Delivered-To: freebsd-questions@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 75A0AD30749 for ; Wed, 5 Apr 2017 16:22:19 +0000 (UTC) (envelope-from lobo@bsd.com.br) Received: from mail-qk0-x242.google.com (mail-qk0-x242.google.com [IPv6:2607:f8b0:400d:c09::242]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (Client CN "smtp.gmail.com", Issuer "Google Internet Authority G2" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 3C6E3DA6 for ; Wed, 5 Apr 2017 16:22:18 +0000 (UTC) (envelope-from lobo@bsd.com.br) Received: by mail-qk0-x242.google.com with SMTP id p22so2337396qka.0 for ; Wed, 05 Apr 2017 09:22:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bsd.com.br; s=capeta; h=date:from:to:cc:subject:message-id:organization:mime-version :content-transfer-encoding; bh=tPwpl3z/i6Jz6IXtSgCjuDCYWD+g5+I+FLmZp5BgRwo=; b=ddbtsvoOJDJLGJmGCk4gBrnhWHyNu5DNuhixCov4rE6TRgX8/+odcQJ9iSR/j6KQpl MLV1vhsAa65AZVC3NktZ82Ft90/WjhvrW/ZV2W5W4gkZpZORu11iHBiWPB3Jrud7ggmn F0RH5KTI/k+E6BdDwyePx3oYmDfp+P8Oz3+WY= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:organization :mime-version:content-transfer-encoding; bh=tPwpl3z/i6Jz6IXtSgCjuDCYWD+g5+I+FLmZp5BgRwo=; b=XnPMHO0esNieRXPwh5IvbtXmUg1E6ZMIpb2VRbZC32u0FOOe1002pyYZv16ZGt2EhL ZO0uK7nJWTNtj1NbPOjSB8hOtodph0gvBmSBUkJy50a99SXTEigly5AruPJEyTESszQP 1B312eWcwHXRXFYbwUcTo9m1bjS5C6W8F/WfR0exEr7K8mCHgrXDQR38JlYRKGYV3JcP m7ONM2Qj3LFzv/Rpnd6CuVU98WDz6vHSIwrl3ukrAe0z8vD3HDo8FdfZj2aCh6GInJvu IMa7dq8J8g5OzgQKwkMctJIcApFOh1yFo1Rkb5VYE7RAnEyNI91ZCWQKJTQUuTq+MPTy N/Tw== X-Gm-Message-State: AFeK/H0rRgzBPdnh2KKwYmsUiIyXi0NY4oJhEfWKmRw7wtEcSujbX0LQXN4cuQfP4evYag== X-Received: by 10.55.168.11 with SMTP id r11mr30608858qke.239.1491409337833; Wed, 05 Apr 2017 09:22:17 -0700 (PDT) Received: from Papi ([177.98.129.212]) by smtp.gmail.com with ESMTPSA id d142sm11069684qkc.32.2017.04.05.09.22.16 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Wed, 05 Apr 2017 09:22:17 -0700 (PDT) Date: Wed, 5 Apr 2017 13:23:16 -0300 From: Mario Lobo To: freebsd-questions@freebsd.org Cc: FreeBSD Hackers Subject: [OFF-TOPIC] C question Message-ID: <20170405132251.536ab064@Papi> Organization: BSD X-Mailer: Claws Mail 3.14.1 (GTK+ 2.24.29; amd64-portbld-freebsd11.0) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 05 Apr 2017 16:22:19 -0000 Hi There ! I don't know if this list is appropriate for this.=20 if it isn't, please point me to the right direction. Since this is for a Freebsd program, I decided to start here, hoping to find some C gurus who can help me out. Here it goes: 1) I have this; typedef struct { ulong me; ulong record; char string[256]; }KFNODE; KFNODE ***Nodes; 2) Then allocate for the pointers; Nodes =3D (KFNODE ***) malloc(5 * sizeof(KFNODE **)); memset( Nodes, 0x00, (5 * sizeof(KFNODE **))); =20 for(a =3D 0; a < 5; a++) { Nodes[a] =3D (KFNODE **) malloc(43 * sizeof(KFNODE *)); memset( Nodes[a], 0x00, (43 * sizeof(KFNODE *))); } 3) Allocate for the structs for(a =3D 0; a < 5; a++) { for(b =3D 0; b < 43; b++) {=20 Nodes[a][b] =3D (KFNODE *) malloc(sizeof(KFNODE)); memset( Nodes[a][b], 0x00, sizeof(KFNODE)); } } =46rom this point on, I can access any Nodes[a<5][b<43]. This works fine as long as I previously know is a 2D array. My question is: The dimensions are not known before hand and have to be found at run time. Suppose I find out I'll need a 3D array, like Nodes[a][b][c]? How can I dynamically change the level of indirection? How can I dynamically switch: ---------------- KFNODE ***Nodes; to KFNODE ****Nodes; ---------------- Nodes =3D (KFNODE ***) malloc(5 * sizeof(KFNODE **)); to Nodes =3D (KFNODE ****) malloc(5 * sizeof(KFNODE ***)); ---------------- and so forth? Is this possible at all? Thanks --=20 Mario Lobo http://www.mallavoodoo.com.br FreeBSD since 2.2.8 [not Pro-Audio.... YET!!] =20 "UNIX was not designed to stop you from doing stupid things,=20 because that would also stop you from doing clever things."