CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    12

    How to reference **ptr_xx to ptr_yy [xx][xx]

    Hi All,

    Good Morning to everyone here. :P

    May i know anyone here can help to answer one of my question. I would like to know how to reference double dimension array with double dimension pointer. ( i think this may sounds silly to someone here )

    I have tried to reference one dimension array with pointer without problem,

    e.g. char xx[100]; char *yy;
    yy = xx;
    sprintf(yy, "You are a nice guy");

    // good result

    However, if i try with 2 dimension pointer to 2 dimension array

    char xx[100][500];
    char **yy;
    yy = xx;

    // not working - cannot compile

    char *xx[100];
    char **yy;
    yy =xx;
    // can compile - but it gives error during excution

    Is there any method to hv the address of pointer same as array declared for 2 dimension memory.


    I do not/cant use "malloc" and "new" due to some reason, can anyone help to answer to my doubt?

    Thanks a millions time.

    James Goh

  2. #2
    Join Date
    Aug 1999
    Posts
    586

    Re: How to reference **ptr_xx to ptr_yy [xx][xx]

    First, look at "std::vector" which is much easier and safer to work with than raw C-style arrays. In any case, an array name always decays to a pointer to the first element of the array. Given your example then:

    char xx[100][500];

    What is stored in this array? Well, another array actually. That is, there are 100 elements in your array, each of which is an array of 500 chars. So, since the name "xx" decays to a pointer to the first element of the array as mentioned above, and that element is an array of 500 chars, then you're looking at this:

    char (*yy)[500] = xx;

    "yy" is a pointer to an array of 500 chars. The syntax is brutal but the underlying idea of an array that stores another array is elegant (and you can carry this idea to any number of dimensions). It takes a little practice to think about it this way (draw your elements out on paper to see) but it becomes self-evident after a short time.

  3. #3
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    12

    Re: Re: How to reference **ptr_xx to ptr_yy [xx][xx]

    Originally posted by Sef
    First, look at "std::vector" which is much easier and safer to work with than raw C-style arrays. In any case, an array name always decays to a pointer to the first element of the array. Given your example then:

    char xx[100][500];

    What is stored in this array? Well, another array actually. That is, there are 100 elements in your array, each of which is an array of 500 chars. So, since the name "xx" decays to a pointer to the first element of the array as mentioned above, and that element is an array of 500 chars, then you're looking at this:

    char (*yy)[500] = xx;

    "yy" is a pointer to an array of 500 chars. The syntax is brutal but the underlying idea of an array that stores another array is elegant (and you can carry this idea to any number of dimensions). It takes a little practice to think about it this way (draw your elements out on paper to see) but it becomes self-evident after a short time.
    Thanks Sef.

    I managed to solve the prob with another approach this afternoon by using casting.

    eg.
    char xx[100][500];
    char **yy;

    yy = (char **) xx;
    yy[0] = "You are a nice guy";
    yy[1] = "Thanks for answer to my question";

    printf("%s", yy[0]);
    printf("%s", yy[1]);

    // it works so far.

    so far so good

    Btw, if i am require to declare a very huge variable , such as e.g. char xx[9000][9000], how could i done with it? The compiler hv no problem to compile . However, when comes to running the exection file, i enconter error such as "cannot write to 0x0xxxxx location". Is it the RAM memory size not enough ? What or how can i increase my memory space for my huge 2 dimension array??

    Million thanks and BestRegards,
    James Goh
    Last edited by James Goh; October 16th, 2003 at 10:06 AM.

  4. #4
    Join Date
    Nov 2002
    Location
    Foggy California
    Posts
    1,245

    Noooooooooooo!!!!

    Originally posted by James Goh
    Thanks Sef.

    I managed to solve the prob with another approach this afternoon by using casting.

    eg.
    char xx[100][500];
    char **yy;

    yy = (char **) xx;
    yy[0] = "You are a nice guy";
    yy[1] = "Thanks for answer to my question";

    printf("%s", yy[0]);
    printf("%s", yy[1]);

    // it works so far.

    so far so good

    Btw, if i am require to declare a very huge variable , such as e.g. char xx[9000][9000], how could i done with it? The compiler hv no problem to compile . However, when comes to running the exection file, i enconter error such as "cannot write to 0x0xxxxx location". Is it the RAM memory size not enough ? What or how can i increase my memory space for my huge 2 dimension array??

    Million thanks and BestRegards,
    James Goh
    No, no, no!!! The program will compile but you will run into execution problems! This will not work because the types are incompatible. On most systems the a char[2][2] array will allocate 4 bytes in succession. A char** is a pointer and occupies 4 bytes each. Each element of y will now point to some other memory location. Let's assume that x is initiallized with all zero values and occupies the memory 0x1000 to 0x1003. We then have the following situation:

    Code:
    char x[2][2];
    char **y = (char**)x;
    
    Memory location: 0x1000  0x1001  0x1002  0x1003  0x1004  0x1005  0x1006  0x1007
    VALUE          : 0       0       0               0       0       0       0
    x              : x[0][0] x[0][1] x[1][0] x[1][1]
    y              : y[0]........................... y[1]..........................
    
    Memory location: 0x0000               0x0001
    y              : y[0][0], y[1][0]     y[0][1], y[1][1]
    Doubly dereferencing y will now lead you to an access violation on most systems. This is likely your problem with the "cannot write to 0x0xxxxx location" error. Sef is absolutely correct. If you must work with C-style arrays, then I suggest using this approach (which is ultimately the same as Sef's just a little more clear to the unexperienced programmer):

    Code:
    typedef char char_array_500[500];
    
    char_array_500   xx[100];
    char_array_500* yy=xx;
    This will now work and is perfectly legal. Now about your other question about allocating large arrays: You will experience a stack overflow. Such arrays need to be allocated dynamically with new or malloc(). Search the C++ FAQ or this forum -- you will find whole discussions on this topic.

    Regards,

    Kevin
    Last edited by KevinHall; October 16th, 2003 at 10:31 AM.

  5. #5
    Join Date
    Nov 2002
    Location
    Foggy California
    Posts
    1,245

    Re: Re: Re: How to reference **ptr_xx to ptr_yy [xx][xx]

    Originally posted by James Goh
    Thanks Sef.

    I managed to solve the prob with another approach this afternoon by using casting.

    eg.
    char xx[100][500];
    char **yy;

    yy = (char **) xx;
    yy[0] = "You are a nice guy";
    yy[1] = "Thanks for answer to my question";

    printf("%s", yy[0]);
    printf("%s", yy[1]);

    // it works so far.

    so far so good
    Oh, I know why your code seems to work. That is because you re-assign the pointers to point to the constant strings. But your program certainly does not do what you think it does. Try inseting the following lines at the end of your program:
    Code:
    printf("%c", x[0][0]);
    printf("%c", x[1][0]);
    You'll see that you print junk values. See my previous post as to why!

    - Kevin

  6. #6
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    12
    Hi Kevin,

    Thanks a million.
    What u mentioned is correct, I get weird character if i do as what u mentioned at ur second post.

    The method u used for 2 dimension array is cool, i will implement it in my application. You have a nice day.

    Best Regards,
    James Goh

  7. #7
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    12

    Unhappy


    Let me give a brief discription of the background of my problem.

    The original software was written in 16 bit Turbo C in early 90s, and my task is to convert it from 16 to 32 bit application . Borland C was choosen because it can almost directly convert the Turbo C 16 bit program code into 32 bit program code ( OWL was used).

    The new application is now written in Borland C version 5.02, it will dynamically explicit loads DLL file written in VC++ version 6 ( as i more familar with VC++, i wrote those addition function in VC and dynamically call by Borland C application).

    It can be compiled and run, however, it will prompt error when "I guess" the program is accessing the memory. This error was not seen in the old Turbo C 16 bit applicaiton. I have the error prompt of "The Instruction at "0x0xxxxxx" referenced memory at "x0x0xxxxxx". The memory couldn not be "written".

    I trace the program during debug mode and notice it prompt this error during memory declaration when using Malloc and API HGlobalAlloc function. Thus, i convert most of the Malloc and HglobalAlloc API into static memory declartion, e.g. char xx[yy]. However, there are some Malloc funtion that are declared to allocate very huge memory location in the program, and i cannt convert it into char xx[yy][zz] as i will run into memory error if the size is too big.

    From your point of view, what /where goes wrong in my program , and how could i remedy or get ride of the error prompt? Thanks ..


    BestRegards,
    JamesGoh

  8. #8
    Join Date
    Aug 1999
    Posts
    586
    From your point of view, what /where goes wrong in my program , and how could i remedy or get ride of the error prompt? Thanks ..
    Sorry for not responding to you sooner (following my first post). Others have already commented however though I haven't read them at length. Kevin Hall for instance was absolutely right to correct your (incorrect) use of casting to eliminate the problem. The way I showed you in my first post was the correct way given your original question (I think Kevin also showed a variation of this using typedefs). Other techniques for creating multidimensional arrays also exist as well, including arrays of pointers. You really would be better off using "std::vector" though as I originally suggested. I think the real issue here however is that you need to brush up on arrays and pointers in general. Forums like this are really geared to answer specific questions, not to provide a complete synopsis on larger topics like arrays. The bottom line is that you shouldn't be coding these things if you don't fully understand them which is self-evident now. Many full tuturials exist on the subject all over the web and in your favorite C++ book. I suggest reading them

  9. #9
    Join Date
    Nov 2002
    Location
    Foggy California
    Posts
    1,245
    Sef, he's converting a Turbo C program from the early 90's. I had that compiler and templates were not supported well. I know it did not support the STL (I don't even know if the STL was widespread or even created at that point). I do agree he needs to learn how to deal with pointers correctly and that the STL would be better for all new programs. Upgrading an old program might be even more of a mess than just correcting the use of pointers though.

    James, if you attach your code to your next message and tell us what line # (and file if there are more than one file) that causes you problems, we might be able to help more. Outside of that, I do recommend getting some books and/or finding some tutorials.

    Good luck!

    - Kevin

  10. #10
    Join Date
    Aug 1999
    Posts
    586
    Originally posted by KevinHall
    Sef, he's converting a Turbo C program from the early 90's. I had that compiler and templates were not supported well. I know it did not support the STL (I don't even know if the STL was widespread or even created at that point). I do agree he needs to learn how to deal with pointers correctly and that the STL would be better for all new programs. Upgrading an old program might be even more of a mess than just correcting the use of pointers though
    Agreed. I only mentioned "vector" so at least he would be aware of it for the future (if he can't apply it now). The fact that he would "solve" the problem by casting though even after a correct solution was provided (which he probably didn't fully understand), and that he's unware of the repercussions of declaring a 9000x9000 array (nor how to do it using "new" or even "malloc"), clearly indicates he's in deep water but doesn't know how to swim. He needs to move back to the shallow end and learn the basic strokes before we can really help him (and certainly before he attempts to modernize whatever he's working on)

  11. #11
    Join Date
    Nov 2002
    Location
    Foggy California
    Posts
    1,245
    True!

  12. #12
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    12
    Originally posted by Sef
    Agreed. I only mentioned "vector" so at least he would be aware of it for the future (if he can't apply it now). The fact that he would "solve" the problem by casting though even after a correct solution was provided (which he probably didn't fully understand), and that he's unware of the repercussions of declaring a 9000x9000 array (nor how to do it using "new" or even "malloc"), clearly indicates he's in deep water but doesn't know how to swim. He needs to move back to the shallow end and learn the basic strokes before we can really help him (and certainly before he attempts to modernize whatever he's working on)
    hi Sef
    I think you have mistaken that i disagree with your solution, no i am not. While i posted my problem in the forum this morning , i was also trying to resolve the array - pointer problem as well. I get to "solve" the problem in the afternoon before i come to know your solution. I would say that your solution is equally cool as Kevin' solution.

    This is my 3rd project on C lanugage (infact the first time in C++ , the previous 2 project was on C 'console based' ) and I agree that i am not very good in programming with pointers, and need more readup in C++ language . However, at the mean time i need some quick solution to meet my date line.

    Infact, i do know about Malloc and New method to do dynamic memory allocation, however, it just didnt work in my present Turbo C++ to Borland C++ project ( it works in 16 bit turbo C using the Malloc ). Thus, i have no choice but to convert the Dynamic allocation to static allocation, and it works! ( or maybe another wrong solution?)

    Now my problem is on memory allocation for the huge memory size , e.g. xx[9000][9000]. (it prompt error in the Malloc or New allocation) , and i am going to implement your solution on monday.

    Kevin, I will send the file to you or post it here on Monday as i am not in office today. Thanks
    James

  13. #13
    Join Date
    Nov 2002
    Location
    Foggy California
    Posts
    1,245
    Originally posted by James Goh
    Kevin, I will send the file to you or post it here on Monday as i am not in office today. Thanks
    Post it. That way someone else may be able to help you if I am not able to or I am not able to right away.

  14. #14
    Join Date
    Nov 2002
    Location
    Foggy California
    Posts
    1,245
    P.S. Is this project for school?

  15. #15
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    12
    No, is not school project. It is inductrial project.

    I try both method, but it cannt compile and give error --
    cannot convert 'char (*)[2000]' to 'char **'

    part of the project as shown below


    char ** sht_pass_group;

    /*** added by james using static allocation ***/
    int first_pin_buf[2200];
    int firpin_grpnum__buf[2200];
    first_pin = first_pin_buf;
    firpin_grpnum = firpin_grpnum__buf;


    /**** malloc method blocked by james

    first_pin = (int*)malloc((i+1)*4);
    firpin_grpnum = (int*)malloc((i+1)*4);

    endo of block ***/


    /***** try out Kevin method ****/
    //typedef char char_array_500[2200];
    // char_array_500 xx[2200];
    // char_array_500* yy=xx;
    // sht_pass_group = yy;

    /**** try out sef method ****/
    char xx[100][2200];
    char (*yy)[2200] = xx;
    sht_pass_group = yy;


    /**** malloc method blocked by james

    sht_pass_group = (char**)malloc((i+1)6*2);

    ***/


    rewind(fp1);
    fseek(fp1, fileposition+2, 0);

    ---
    ---

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured