How can I tell the end of an array by a pointer?

Updated on technology 2024-06-12
22 answers
  1. Anonymous users2024-02-11

    It can be judged by special characters, if you can't use special characters, measure the length of the array first, and then use the length to add the array address, if the pointer exceeds this value, it means that the array has ended.

    For example: int a[5] = ;

    int* pb,pt ;PB is used to move, and PT records the address of the last unit of the array.

    pt = a + sizeof(a)/sizeof(int) ;

    for( pb = a ; pb < pt ; pb++

  2. Anonymous users2024-02-10

    int *p,k=0;

    After the array is initialized, the unassigned array elements are automatically assigned 0 or empty ('\0');

    int a[10]=;There are 6 elements in total, a[0] a[5], a[5] followed by a[6] a[9] are all 0,'\0'ASCII code of the terminator ==0;

    p=a;The pointer p points to array a;

    while(*p!='\0') or while(*p), which means that the array element pointed to by the pointer p is 0 ('0) The cycle ends.

    k++p++;The pointer moves to the right.

  3. Anonymous users2024-02-09

    You can use a judgment statement to determine whether the pointer understands that you are equal to a null value, and if it is a null value, it means that the array has ended.

  4. Anonymous users2024-02-08

    You can only calculate the size of the pointer array, for example, if you define the pointer array char *ch=, you can use sizeof(ch) sizeof(char); Calculate how much space it occupies. In the future, it must be limited to the calculated value.

  5. Anonymous users2024-02-07

    1. Explicitly pass the array length, for example.

    void sot(int a, int n)2, implicitly passing the length of the array.

    1) Store metadata in front of the array.

    For example, if you want to store a string with length n=255, you can use an array of length n+1. The first element of the array stores the length of the string, followed by the contents of the string.

    2) Use a special element to mark the end of the array at the end of the array.

    The most typical example of this is the use of the word nul('\0'An array of characters ending in characters represents a string. It is also common to mark the end of a pointer array with a null pointer.

    In the C language, if the function parameter only has an array pointer, the length of the array cannot be determined.

    How does C get the length of an array?

    The total length is filled with the length of a single one, that is, the number of arrays.

    int len = sizeof(unstartstring)/sizeof(unsigned int);

  6. Anonymous users2024-02-06

    You should know that when the struct allocates space, it says the number of applications, that is, the number you want; students is the pointer, how do you not know the number of assigned? Even if someone else has applied for it, it should tell you this information.

    Either you use a linked list to say that each item is connected in series, and the last one is set to the end flag;

    Add a next pointer, and the next of the last student points to null. Even linked lists are judged by the next pointer being null to the end.

    Either build an array of structs and record them. Either make a linked list.

    You don't need a linked list, but it's not as convenient as arrays. Remember that array (pointer) out-of-bounds is always the most difficult problem in C. Therefore, there is a clause in the project coding specification:

    When manipulating arrays, you must judge the boundary, and the best way to do this is to record the number of members.

  7. Anonymous users2024-02-05

    This can be achieved in a way like this:

    #include<

    #include

    int main()

    char *a=

    int i;

    for(i=0; iprintf("%s",a[i]);

    return 0;

  8. Anonymous users2024-02-04

    For example, the pointer array in the question doesn't need to add anything to determine whether it is over or not. Since the size of the array is fixed, you can use the sizeof function to find the number of elements in the array.

    size = sizeof(a) sizeof(char *) indicates the number of array elements.

    char*a=

    Iterate through the array. for(int i = 0; i < size; i++)puts(a[i]);

  9. Anonymous users2024-02-03

    There is no special way to judge the end of a two-dimensional array in the C language, because you define it yourself, you should know how many variables there are in this array, so you need to judge it according to your own definition.

  10. Anonymous users2024-02-02

    In this way, the system will automatically add a '0' ending character to the end of the string. Look at the books, they talk about string initialization.

  11. Anonymous users2024-02-01

    char *a=

    This way you know that it ends with a null pointer.

    Or, you record its length with an additional variable.

  12. Anonymous users2024-01-31

    When scanf input, the input ends with a space enter, and if the line breaks at the end of the carriage return, an unpredictable error will be reported; scanf() is easy to out-of-bound, and it is very unsafe to do user interaction programs!

  13. Anonymous users2024-01-30

    sizeof(a) to get the size of the pointer array.

    sizeof(a) sizeof(char *) to get the number of pointers.

  14. Anonymous users2024-01-29

    How to use a two-dimensional array to input a string with an indeterminate number of lines in C Help!! I was able to use eof to end the input code, but what I wrote was wrong.。。。 Seek help!!! I was able to use eof to end the input code, but what I wrote was wrong.。。。

  15. Anonymous users2024-01-28

    You should insert a special string at the end of the array, such as:""to mark the end of it. There doesn't seem to be a good way to do it at the linguistic level.

  16. Anonymous users2024-01-27

    The implementation of this function should be written by you, but you don't seem to understand the meaning of the interface, according to the function name and parameter name, you can roughly infer that this is a function to judge the difference between two numeric arrays, and the following n refers to the size of these two arrays, so the implementation of the program should be:

    bool verify(int before,int after,int n)

    elseif (i == n)

    As for your problem, n+1 should cause the array subscript to go out of bounds, and the exception is easy to explain.

  17. Anonymous users2024-01-26

    The end of the string is'\0', arrays can only be used with sizeof(after) sizeof(int).

  18. Anonymous users2024-01-25

    When passing an array, only the pointer is passed, so sizeof(after) is 4 or 8, which is the pointer length, depending on how many bits you have in the system.

  19. Anonymous users2024-01-24

    After is a form parameter, the function does not know the size of the array, and uses "sizeof(after) sizeof(int); "Can you get the array size?

    It seems that the shape parameter n should be the size of the array?

  20. Anonymous users2024-01-23

    I think it's a version problem.,I remember in the past on win that there was no problem.,But now it's a problem to compile with gcc under linux.,When debugging, it frequently says no symbol.,After the function returns, there is a prompt that says segment fault......It is estimated that only the function that writes the file has this error, and the function that reads the file does not. It is probably a version or coding issue......(I'm using UTF-8 under Linux),But the output is no problem.,It's still done what should be done.,It's just that it doesn't seem to be normal at the end.,Maybe it's caused by some problems with this version of the standard library, right? (Didn't look at the ...... of its implementation.)Also, I didn't get an error (I used bash) on the command line......The completed function is normal.

  21. Anonymous users2024-01-22

    if(fscanf(fp, ,==eof)

    With this statement, if the file ends, the fscanf function returns a value of eof (equal to -1).

  22. Anonymous users2024-01-21

    With the int feof(file *fp) function, a return value of 1 indicates that the end has been reached, and a value of 0 indicates that the end has not been reached.

    You still say what the function of jsvalue() is, you I don't understand, and the array name is a pointer that can't be moved, and your A++ should be problematic.

Related questions
14 answers2024-06-12

function assoc_unique($arr, $key) {

tmp_arr = array(); >>>More

10 answers2024-06-12

Here's a C implementation that assigns an array a[10] to p[n]: >>>More

5 answers2024-06-12

Ok, here's the **:

#include >>>More

7 answers2024-06-12

In addition, make an array, arraylist will do.

A loop, put the original array of things into it one by one, and judge it before, if there is one, don't put it. >>>More

13 answers2024-06-12

Programs that have been debugged. Hope it helps.

import ; >>>More