How to iterate over a dynamic array with a C pointer

Updated on technology 2024-04-21
17 answers
  1. Anonymous users2024-02-08

    #include

    #include

    struct book

    char book_id[20];

    char book_name[20];

    char book_writer[20];

    struct book* next;

    void main()

    int i;

    struct book * qwe,*tep,*head;

    qwe = (struct book *)malloc(sizeof(struct book));

    head=qwe;

    for(i=0;i<2;i++)

    printf("Please enter the book ID%3D",i+1);

    scanf("%s",&qwe->book_id);

    printf("Please enter the book name %3d",i+1);

    scanf("%s",&qwe->book_name);

    printf("Please enter the author name %3d",i+1);

    scanf("%s",&qwe->book_writer);

    tep= (struct book *)malloc(sizeof(struct book));

    qwe->next=tep;

    qwe=tep;

    qwe->next=null;

    printf("Output book information");

    for(i=0;i<2;i++)

    printf("Book id%sBook name%sBook author name%s",head->book_id,head->book_name,head->book_writer);

    head=head->next;

    printf("");

  2. Anonymous users2024-02-07

    It can be written like this.

    int n;

    printf("Please enter you.

    The size of the array you want to define:");

    scanf("%d",&n);

    int *a = (int *)malloc(n*sizeof(int));Apply for n integer data spaces, and the header file is.

    if(a == null) returns -1memset(a, 0, n) if the application fails. The space applied for initialization is 0, and the header file is 0.

    However, dynamic arrays cannot be defined.

  3. Anonymous users2024-02-06

    1. Use the malloc function to allocate the size of the first dimension first, and then allocate the size of each dimension in a loop.

    2. Example, assign 3 rows and 4 columns of 2D array:

    #include

    #include

    int main()

    Initialize. for (i = 0; i < 3;+i)} Output test.

    for (i = 0; i < 3; +i)printf ("");

    Free up dynamic space.

    for (i = 0; i < 3; +i)free(a);

    return 0;}/*

    Output: 0 1 2 3

  4. Anonymous users2024-02-05

    int size=5;The size of the array can be defined by you or entered by the user.

    char *p=new char[size];or int and other types like that.

    After that, it is good to use p as an array, for example, p[0] is the 0 element of the array.

  5. Anonymous users2024-02-04

    Let's say you want to define.

    The number zhi group of du is int a[n], and the dynamic dao refuge method is answer:

    #include

    int main()

  6. Anonymous users2024-02-03

    You see, as you type, you assign a value to the array, and at the end, p points to a[9], and then you go to printf.

    p++, where p does not point to a, but to a[9]; , and then look at your second program, which is also wrong, how can it be run out, p likewise without a definite point, on my computer.

    Doesn't run. That's how it should be.

    #include

    int main()

    int *p,i,a[10];

    p=a;for(i=0;i<10;i++)p++=i;

    p=a;for(i=0;i<10;i++)printf("a[%d]=%d",i,*(p+i));

    Your second program runs out. Although the answer is the same, however, it is a random value given by the system, which is not correct.

    Hope I can help you

  7. Anonymous users2024-02-02

    p++, you're going to look at the priority of the operator, although I also kind of forgot about it, but in any case, *p++ can't be right, whether it's *(p++) or (*p)++ can't have the same value as *p.

    Of course, *p++ means *(p++) so p has moved on to the next position.

  8. Anonymous users2024-02-01

    #include

    void print(int *p, int len)}int main()

    I was dizzy, and I didn't even adopt it.

  9. Anonymous users2024-01-31

    e.g. int a[10];

    int *p=a;

    If you traverse it directly, it is.

    for(int i=0;i<10;i++)cout, and pointer traversal is like this.

    for(int i=0;i<10; i++)

  10. Anonymous users2024-01-30

    Pointer solution traversal array example:

    #include

    int main();

    int i, *p = arr, len = sizeof(arr) / sizeof(int);

    for(i=0; iprintf("%d ", *p+i) )printf("");

    return 0;

    Arrays are just simple arrangements of array elements in memory, with no start and end flags, and you can't use sizeof(p) sizeof(int) when finding the length of an array, because p is just a pointer to type int, and the compiler doesn't know whether it's pointing to an integer or a series of integers (arrays).

    So sizeof(p) takes the number of bytes occupied by the p pointer variable itself, not the number of bytes used by the entire array. That is to say, according to the array pointer, the number of elements of the entire array cannot be reversed, and the information of the array from the beginning to the end of the array.

  11. Anonymous users2024-01-29

    #include

    void print(int *p, int len)}int main()

    I was dizzy, and I didn't even adopt it.

  12. Anonymous users2024-01-28

    vs2005, put int i; After putting it in front, it is normal.

    First of all, it is the standard of C99 to define variables when it is used in pure C.

    VS2005 is partially supported by the C99 standard, which is used in VS2005. C files are not supported by default, your problem should be that your file name is. cpp, which is not good for debugging c programs.

    The reason for the error is that you have already traversed it once with the pointer p, and the address of p has been changed to a+5, no longer a, and then continue to access it belongs to cross-border access, if it is not hanging, the display value is the value in memory, and the value is random.

  13. Anonymous users2024-01-27

    You've already used *(p++) earlier, and the pointer is now pointing to the cell behind a[4]. And the value of this storage cell is uncertain. The landlord used P++ to loop the output later, of course, it was wrong.

    A sentence p=a should be added in the middle; The pointer then points to a[0] again. Does the landlord understand?

  14. Anonymous users2024-01-26

    for(int i=0;i<5;i++)

    Wrong! i should be defined in front !

    int a[5]=;

    int *p=a,i;

    for(i=0;i<5;i++)

    That's it.

  15. Anonymous users2024-01-25

    No problem! Look at my stickers. I'm using cfree

  16. Anonymous users2024-01-24

    What compiler, I gcc is fine here.

  17. Anonymous users2024-01-23

    No problem.

    I just defined i separately before for.

Related questions
7 answers2024-04-21

MF47 multimeter measures and measures DC voltage: the maximum allowable measurement voltage of its ordinary board surface is 1000V. Another special terminal with a measuring voltage of up to 2500V (not recommended for general operators). >>>More

19 answers2024-04-21

In Final Cut Pro 7, there are several ways to create multiscreen transitions. Here are some basic ways to do it: >>>More

12 answers2024-04-21

Just add something sparkling and you're good to go

6 answers2024-04-21

The first method: first import the sci file, and then click on the file-import in the menu bar and then select file or folder. >>>More

17 answers2024-04-21

The operation to delete songs in iPod with iTunes is as follows: >>>More