Sponsored Content
Top Forums Programming Pointer for 2D array seems to be 3D in C Post 302999243 by yifangt on Thursday 15th of June 2017 07:07:42 PM
Old 06-15-2017
Pointer for 2D array seems to be 3D in C

I am struggling with the pointer to 2D-array (cf: 2D array of pointers). Can anybody help me elaborate how the pointer x moves in the memory to access the individual of y[2][6], especially the high lighted lines?
I have talked to one of the curators of the forum, but I am still not quite clear.
Here is my code:
Code:
#include<stdio.h>

int main(void)
{
    int (*x)[2][6];                 //pointer for integers array in size of 2x6 (2 rows x 6 columns),
                                    //.i.e the array is always with size of 12?
//    int (*a[8])[5];                 //Line 9: a is a pointer array of size 8, each for integer array of size 5 

    int y[2][6] = {{11,12,13,14,15,16},
                   {21,22,23,24,25,26}};    //2D array of integers
    int *z;                      //pointer to integer
    int i;

    z = y[0];
    for(i = 0;i<6;i++)
        printf("%d ",z[i]);
    printf("\n");

    x = &y;    // More complicated situation for me!
    x = y;     // Warning: incompatible pointer type.
//    x[0][0] = y[0][0];     // won't work
        printf("   (x[0][0]): %p\n",  x[0][0]);
        printf("  *(x[0][0]): %d\n",*(x[0][0]));        //Q1a
        printf("  x[0][0][0]: %d\n",x[0][0][0]);        //Q1b
        printf("*(x[0][0]+1): %d\n",*(x[0][0]+1));      //Q1c
//        printf("*(x[0][0]+2): %d\n",*(x[0][0]+2));
//        printf("*(x[0][0]+3): %d\n",*(x[0][0]+3));
//        printf("*(x[0][0]+4): %d\n",*(x[0][0]+4));
        printf("*(x[0][0]+5): %d\n",*(x[0][0]+5));

        printf("    x[0][1]: %p\n",   x[0][1]);        //Q2a
        printf("    *x[0][1]: %d\n",*(x[0][1]));
        printf("  x[0][1][0]: %d\n",x[0][1][0]);       //Q2b 
        printf("*(x[0][1]+1): %d\n",*(x[0][1]+1));     //Q2c 
//        printf("*(x[0][1]+2): %d\n",*(x[0][1]+2));
//        printf("*(x[0][1]+3): %d\n",*(x[0][1]+3));
        printf("*(x[0][1]+4): %d\n",*(x[0][1]+4));
        printf("*(x[0][1][4]): %d\n",*(x[0][4]));
        
    printf("&y: %p\n", &y);
    printf(" y: %p\n",  y);
  
    printf(" x: %p\n",  x);
    printf("&x: %p\n", &x);
    
    return 0;
}

1) Although y and &y are the same, but x = y issues warning;
2) Q1a/Q2a is the part I think I understand which is the first element of each row of y.
3) but Q1b/c, and Q2b/c turns out to be 3-D to me.
Can anybody give me a diagram how pointer x moves in the memory for each member of y?
4) Line 9: int (*a[8])[5]; is related, and I put it here for future reference but skip it at this moment.

Thanks a lot!
 

10 More Discussions You Might Find Interesting

1. Programming

pointer

void main() { int a={1,2,3,4,5,6,7,8,9,10}; int *p=a; int *q=&a; cout<<q-p+1<<endl; } The output is 10, how? if we give cout<<q it will print the address, value won't print.... if we give cout<<p it will print the address, value won't print.... p has the base addr; q... (1 Reply)
Discussion started by: sarwan
1 Replies

2. UNIX for Dummies Questions & Answers

Storing pointer array in C

All .. I am having a pointer array . And trying to store the addess into that pointer array . please see below the problem i faced code: int cnt1; char *t_array; char *f_array; for(cnt1=0; cnt1<1000; cnt1++) { t_array =... (1 Reply)
Discussion started by: arunkumar_mca
1 Replies

3. Programming

far pointer

what is far pointer in C (1 Reply)
Discussion started by: useless79
1 Replies

4. Programming

pass a pointer-to-pointer, or return a pointer?

If one wants to get a start address of a array or a string or a block of memory via a function, there are at least two methods to achieve it: (1) one is to pass a pointer-to-pointer parameter, like: int my_malloc(int size, char **pmem) { *pmem=(char *)malloc(size); if(*pmem==NULL)... (11 Replies)
Discussion started by: aaronwong
11 Replies

5. Programming

C pointer/array duality confusion

Hi all, Can anyone provide help with getting the right syntax regarding array/pointers in C in the following code? Can't locate a specific example which clarifies this... Say I declare a typedef to an array of pointers to some type... /** * An array of ptrs to sections */ typedef... (4 Replies)
Discussion started by: gorga
4 Replies

6. Programming

help with char pointer array in C

i have an array like #define NUM 8 .... new_socket_fd = accept(socket_fd, (struct sockaddr *) &cli_addr, &client_length); char *items = {"one", "two", "three", "four", "five", "six", "seven", "eight"}; char *item_name_length = {"3", "3", "5", "4", "4", "3", "5", "5"}; ... (1 Reply)
Discussion started by: omega666
1 Replies

7. Programming

structure pointer array as function parameters

if i create an array of pointers to a structure "struct node" as: struct node *r; and create "n" number of "linked lists" and assign it to the various struct pointers r using some function with a return type as structure pointer as: r=multiplty(.......) /*some parameters*/ is... (2 Replies)
Discussion started by: mscoder
2 Replies

8. Programming

Pointer and address

This code is to print out the program name and arguments list one by one: 1 #include<stdio.h> 2 3 void main(int argc, char *argv) 4 { 5 int iCount = 0; 6 while (iCount < argc) { 7 printf("argc:%d\t%s\n",iCount, argv); 8 ... (14 Replies)
Discussion started by: yifangt
14 Replies

9. Programming

Unclear pointer and array

Hello, The purpose of the program is to print a sub string from the prompt inputs. I do not understand why char pointer does not work but char array will for line 40 and Line 41. ./a.out thisisatest 0 8 substring = "thisisat"And my code is: #include <stdio.h> #include <stdlib.h> #include... (29 Replies)
Discussion started by: yifangt
29 Replies

10. Programming

Character pointer to Character array

how to copy content of character pointer to character array in c programming.. char *num; char name=num; (1 Reply)
Discussion started by: zinat
1 Replies
backtrace(3)						   BSD Library Functions Manual 					      backtrace(3)

NAME
backtrace, backtrace_symbols, backtrace_symbols_fd -- call stack backtrace and display functions SYNOPSIS
#include <execinfo.h> int backtrace(void** array, int size); char** backtrace_symbols(void* const* array, int size); void backtrace_symbols_fd(void* const* array, int size, int fd); DESCRIPTION
These routines provide a mechanism to examine the current thread's call stack. backtrace() writes the function return addresses of the current call stack to the array of pointers referenced by array. At most, size pointers are written. The number of pointers actually written to array is returned. backtrace_symbols() attempts to transform a call stack obtained by backtrace() into an array of human-readable strings using dladdr(). The array of strings returned has size elements. It is allocated using malloc() and should be released using free(). There is no need to free the individual strings in the array. backtrace_symbols_fd() performs the same operation as backtrace_symbols(), but the resulting strings are immediately written to the file descriptor fd, and are not returned. EXAMPLE
#include <execinfo.h> #include <stdio.h> ... void* callstack[128]; int i, frames = backtrace(callstack, 128); char** strs = backtrace_symbols(callstack, frames); for (i = 0; i < frames; ++i) { printf("%s ", strs[i]); } free(strs); ... HISTORY
These functions first appeared in Mac OS X 10.5. SEE ALSO
dladdr(3), malloc(3) Mac OS X February 15, 2007 Mac OS X
All times are GMT -4. The time now is 01:02 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy