pointer problem

This is a discussion on pointer problem within the C Programming forums, part of the General Programming Boards category; Hi all, I have a problem with this code. Can somebody explain to me what this line does p = ...

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    31

    pointer problem

    Hi all,
    I have a problem with this code. Can somebody explain to me what this line does p = (char (*)[SZ])in;
    I know that "in" is beeing casted but its strange when i see it with pointers. What is exactly happening in memory, what is p pointing at?

    And also what is the difference between
    char (*p)[SZ]; and char *p[SZ];

    thx.
    Code:
    #include <stdio.h>
    
    #define SZ 3
    char print(char c) {
       return c >= ' ' && c <= '~' ? c : '#';
    }
    
    void fooey(char *in, char *out, int size) {
       char (*p)[SZ]; /*p points to array[s] of SZ chars each */
       int r, c, i;
    
       p = (char (*)[SZ])in;
    
       for(c = i = 0; c < SZ; c++)
          for(r = 0; r < size/SZ; r++, i++) {
    		 out[i] = p[r][c] && p[r][c] != ' ' ? (p[r][c] | ' ') + 1 : p[r][c];
    		 printf("%d=%c,(%d,%d)=%c\n", i, print(out[i]), r, c, print(p[r][c]));
           }
    }
    
    int main( ) {
       char test[9] = "NFCGNX N",
    	test2[sizeof test];
    
       fooey(test, test2, sizeof test);
       printf("%s\n", test2);
       return 0;
    }

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    The cast is used because 'in' is a pointer to char. It may be indexed like an array, but only in one dimension. The cast allows 'p' to be indexed as a two-dimensional array. For example, you can't do the following.
    Code:
    void fooey(char *in, char *out, int size) {
       int r, c, i;
       for(c = i = 0; c < SZ; c++)
          for(r = 0; r < size/SZ; r++, i++) {
    		 out[i] = in[r][c] && in[r][c] != ' ' ? (in[r][c] | ' ') + 1 : in[r][c];
    		 printf("%d=%c,(%d,%d)=%c\n", i, print(out[i]), r, c, print(in[r][c]));
           }
    }
    Using 'p' allows the two-dimensional indexing. But I don't believe this is in strict conformance with the standard (reference).

    >And also what is the difference between
    >char (*p)[SZ]; and char *p[SZ];
    Code:
    char (*p)[SZ]; /* p is a pointer to an array of SZ chars */
    char  *q [SZ]; /* q is an array of SZ pointers to char */
    More info can be found here (near the bottom). It is a chapter from A TUTORIAL ON POINTERS AND ARRAYS IN C, which contains lots of good information. And even more information can be found here.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointer to pointer realloc problem
    By prakash0104 in forum C Programming
    Replies: 14
    Last Post: 04-06-2009, 08:53 PM
  2. Another pointer problem
    By mikahell in forum C++ Programming
    Replies: 21
    Last Post: 07-20-2006, 07:37 PM
  3. Pointer problem
    By mikahell in forum C++ Programming
    Replies: 5
    Last Post: 07-20-2006, 10:21 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. pointer problem
    By DMaxJ in forum C Programming
    Replies: 4
    Last Post: 06-11-2003, 12:14 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21