c Dynamic allocation of two-dimensional arrays of structural bodies

Source: Internet
Author: User

I have been struggling with this problem for a long time, because I have been busy (myself lazy), so I have not been able to study this well. I hope this article will help you.

1#include <stdio.h>2#include <stdlib.h>3#include <stddef.h>4 5typedefstructLnode {6     intF;7     structlnode*Next;8}lnode, *linklist;9 intMain ()Ten { Onelnode** map = (Lnode * *)malloc(5*sizeof(lnode*));//allocate 5 struct pointer space A      for(inti =0; I <5; ++i)//this loop allocates the corresponding space to 5 pointers. -     { -Map[i] = (Lnode *)malloc(Ten*sizeof(Lnode));//Allocate 10 node space the     } -  -      for(inti =0; I <5; ++i) -          for(intj =0; J <Ten; ++j) +         { -(Map[i] + j)->f =J; +         } A      for(inti =0; I <5; ++i) at     { -          for(intj =0; J <Ten; ++j) -         { -printf"%d\t", (Map[i] + j)F); -         } -printf"\ n"); in     } -     return 0; to}
Example One:

Example one thought: 1, the allocation of structural body pointer space;

2. Allocate the corresponding node space for the place where the pointer refers.

1#include <stdio.h>2#include <stdlib.h>3#include <stddef.h>4 5typedefstructlnode{6     intF;7     structlnode*Next;8}lnode,*Plnode;9 intMain ()Ten { Onelnode** map = (Lnode * *)malloc(5*sizeof(lnode*)); ALnode *tmap = (Lnode *)malloc(5*Ten*sizeof(Lnode)); -      for(inti =0; I <5; ++i) -     { theMap[i] = (Lnode *) (Char*) TMap + i *Ten*sizeof(Lnode));  -     } -      for(inti =0; I <5; ++i) -          for(intj =0; J <Ten; ++j) +(Map[i] + j)->f =J; -      for(inti =0; I <5; ++i) +     { A          for(intj =0; J <Ten; ++j) at         { -printf"%d\t", (Map[i] + j)F); -         } -printf"\ n"); -     } -      Free(TMAP); in      Free(map); -     return 0; to}
Example Two:

Example two ideas: 1, the allocation of structural body pointer space;

2, the allocation of the corresponding number of nodes space;

3. Use an array of pointers to split.

Note: I was just beginning to write "map[i" = TMap + i * * sizeof (LNODE); " So, since TMap here is the lnode struct pointer, he moves to "I * * * sizeof (Lnode)" *sizeof (Lnode). Give an example int *a; So a +3; Is the "A-point address" + 3*sizeof (int). So, if you want to add this, you can convert the TMAP to the address of the char* type in addition, and then force the type conversion, as shown in the code map[i] = (Lnode *) ((char *) TMAP + i * * sizeof (Lnode)). Of course, such as map[i] =tmap + i * 10.

1#include <stdio.h>2#include <stdlib.h>3#include <stddef.h>4 5typedefstructlnode{6     intF;7     structlnode*Next;8}lnode,*Plnode;9 intMain ()Ten { Onelnode** map = (Lnode * *)malloc(5*sizeof(lnode*) +5*Ten*sizeof(Lnode)); ALnode *head = (Lnode *) (map +5);//This is equivalent to the address indicated by map plus 5*sizeof (lnode*) -      for(inti =0; I <5; ++i) -     { theMap[i] = head + i *Ten;  -         //Here the principle is similar, must understand int *a, then A + 3 refers to the address is "a point to the address" + 3*sizeof (int).  -          -     } +      for(inti =0; I <5; ++i) -          for(intj =0; J <Ten; ++j) +(Map[i] + j)->f =J; A      for(inti =0; I <5; ++i) at     { -          for(intj =0; J <Ten; ++j) -         { -printf"%d\t", (Map[i] + j)F); -         } -printf"\ n"); in     } -      Free(map); to     return 0; +}
Example Three:

Here is a direct allocation of a large space, and then use the pointer to split. As long as we understand the previous two, it's not difficult.

The name does not come from: http://www.cnblogs.com/mingbujian/p/4781622.html This article copyright belongs to the author, welcome to reprint, but without the author's consent must retain this paragraph statement, and in the article page obvious location to give the original text connection.

c Dynamic allocation of two-dimensional arrays of structural bodies

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.