0

Please have a look at the following code and tell me where does ***ptr locates ? i.e. i have a feelings that ***ptr actually locates at ptr[0][0][0] Am I wrong ? The following is a 3d representation of pointer. where I am trying to assign some characters and later i wanted to test what is the index of ***ptr? will be waiting

#include<stdio.h>
#include<conio.h>
#define row 5
#define rw 3
#define col 10


char ***ptr;
int i,j,k;


void main()
{

clrscr();

ptr=(char *)malloc(row*sizeof(char *));

for(i=0;i<row;i++)
    {
        *(ptr+row)=(char *)malloc(rw*sizeof(char *));
        printf("\t:\n");

           for(j=0;j<rw;j++)
           {
           *(*(ptr+row)+rw)=(char *)malloc(col*sizeof(char *));
           if(i==0 && j==0)
               {       //   *(*(ptr+row)+rw)="kabul";
            **ptr="zzz";

               }
           else
            *(*(ptr+row)+rw)="abul";
           printf("\taddress=%d %d%d = %s\n",((ptr+row)+rw),i,j,*(*(ptr+row)+rw));

           }

         printf("\n");
    }


printf("%c %d",***ptr,ptr);
getch();
}
4
  • you are doing one thing wrong (2 times). assigning memory to location *(ptr+row) writes it to one place (only one even though it's a loop. you need to write it to *(ptr+i) (repeat with all mallocs in loops Commented Oct 21, 2011 at 20:57
  • dude i m trying to implement a 3d array here. so m i not supposed to have 2d loop for the 2nd index and the 3rd index and ending up with writing malloc 3 times in the code ? Commented Oct 21, 2011 at 21:02
  • Do you try to create a two-dimensional array of string perhaps? From your code I am not sure what you are trying to do... Commented Oct 21, 2011 at 21:17
  • ptr[0][0]="abul" means ptr[0][0][0]=='a' in that case i have created a 3d array. what i m trying know is what is the equivalent indexing for ***ptr??? as we can assign ***ptr='p' where is that ? There has to be an indexing point for it ... is not it ? Commented Oct 21, 2011 at 21:20

1 Answer 1

2

First of all, I find your coding style extremely hard to read.

Answering your question, yes, ptr[0][0][0] is a synonym of ***ptr. Thats because a[b] is by definition equal to *(a+b), so ptr[0] is equal to *ptr, etc.

Said that, here is my version of your code:

#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#define row 5
#define rw 3
#define col 10


char ***ptr;

int main()
{
    int i, j;

    ptr = (char***)malloc(row * sizeof(char **));

    for(i = 0; i < row; i++)
    {
        ptr[i]= (char**)malloc(rw * sizeof(char *));
        printf("\t:\n");

        for(j = 0; j < rw; j++)
        {
            ptr[i][j] = (char*)malloc(col * sizeof(char));
            if (i == 0 && j == 0)
            {
                strcpy(ptr[i][j], "zzz");
            }
            else
            {
                strcpy(ptr[i][j], "abul");
            }
            printf("\taddress=%p %d,%d = %s\n", ptr[i][j], i, j, ptr[i][j]);

        }
        printf("\n");
    }
    return;
}

Note the following changes:

  • Never write void main in C or C++. And throw away any book that prints it.
  • The argument of malloc is usually the number of elements times the size of the element. Place special attention to the real type that you intend to use.
  • The return of malloc is usually cast to the type pointer-to-the-type-of-the-element.
  • The index in the arrays should be i and j, not row and rw.
  • Why all the *(ptr + x) stuff? That's why we have the ptr[x] syntax.
  • You probably want to use strcpy to fill your strings, but difficult to say without explaining the problem.
  • When you want to printf a pointer, use %p.
  • If you use malloc, include <stdlib.h>.
  • Prefer local variables (i, j) to global ones, particularly for loops.
  • And a few other minor changes here and there...

PS. <conio.h>? Really? Are you still using Turbo-C or what?

Sign up to request clarification or add additional context in comments.

2 Comments

Didn't you know, MS-DOS devs are in high demand right now! ;)
@Maverick_Mrt Nobody is making fun of you, but if you don't want your code to be criticized you shouldn't post it on the Internet.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.