Trying to find different methods of accessing array elements?

时间:2021-09-30 01:38:31

I am new to C but determined to learn good C. I need your help. While practising arrays, I just thought to find out that the way I am accessing array element is right or maybe there might be a better way to do it. Please check the following code and suggest a good way of using array with pointers. I would really appreciate if somebody can guide me to a tutorial for advance practice of array with pointers.

我是C的新手但决心要学好C.我需要你的帮助。在练习数组时,我只是想知道我访问数组元素的方式是正确的,或者可能有更好的方法来实现它。请检查以下代码并建议使用带指针的数组的好方法。如果有人可以指导我使用指针进行数组的高级练习,我将非常感激。

int main()
{
   unsigned int i, j;
   unsigned int arr1[] = {1,2,3,4,5,6,7,8,9};

   unsigned int * ptr_arr = (unsigned int *)malloc(sizeof(unsigned int));
   ptr_arr = arr1;

   for(i=0;i<=8;i++)
   {
         printf("Value at location %d\t is:  %d\n",i,*(ptr_arr+i));
         printf("\n");         
         printf("Value of i %d and value at %d\n",i, ptr_arr[i]);
   }

   return 0;
}  

4 个解决方案

#1


4  

You program is leaking memory

你的程序是泄漏内存

unsigned int * ptr_arr = (unsigned int *)malloc(sizeof(unsigned int));

unsigned int * ptr_arr =(unsigned int *)malloc(sizeof(unsigned int));

There is no need to allocate memory for ptr_arr.

不需要为ptr_arr分配内存。

assignment of address of arr1 is enough

arr1的地址分配就足够了

ptr_arr = arr1;

ptr_arr = arr1;

Another way to use it is increment the pointer every time and use it

另一种使用它的方法是每次递增指针并使用它

    ptr_array2 = arr1
    for (int i = 0; i < 8 ; i++)
    {
       printf ("%d", *ptr_array2);
       ptr_array2++;
     }

#2


1  

Both ways of accessing an element in the array are valid. But for readability sake, use array[subscript] instead of *(arrayStart+index).

访问数组中元素的两种方式都是有效的。但为了便于阅读,请使用array [subscript]而不是*(arrayStart + index)。

Example:

int array[10] = {1,2,3,4,5,6,7,8,9,0};

In an array, the elements are contiguously placed, beginning at array(say address 0x00000000). Assuming a 32 bit integer size, 2 is placed at address of array + 4, which is at address 0x00000004, similarly the next element 3 is at address array + 8, which is address 0x00000008 and so on.

在数组中,元素是连续放置的,从数组开始(比如地址0x00000000)。假设32位整数大小,2位于数组+4的地址,位于地址0x00000004,类似地,下一个元素3位于地址数组+8,即地址0x00000008,依此类推。

When I write array[2] I mean I want the second element after the element at array(or array[0]) which is actually the 3rd element in the array. Hence, array indexes start at 0.

当我写数组[2]时,我的意思是我想要在数组(或数组[0])之后的第二个元素,它实际上是数组中的第3个元素。因此,数组索引从0开始。

It literally means, array[0] = *(array + 0), array[1] = *(array + 1), array[2]=*(array+2)

字面意思是,array [0] = *(array + 0),array [1] = *(array + 1),array [2] = *(array + 2)

#3


0  

You need not allocate memory for ptr_arr as you are anyway assigning the address of arr to ptr_arr. Your above code will lead to a memory leak.

您无需为ptr_arr分配内存,因为您无论如何都要将arr的地址分配给ptr_arr。您的上述代码将导致内存泄漏。

You can refer to one of numerous tutorials available in the internet. Just for reference I am posting a few links:

您可以参考互联网上提供的众多教程之一。仅供参考,我发布了一些链接:

http://www.clear.rice.edu/comp221/html/pdf/03-arrays-pointers.pdf http://www.cs.utexas.edu/users/fussell/courses/cs310h/lectures/Lecture_17-310h.pdf

#4


0  

You missed one method:

你错过了一种方法:

     printf("Value of i %d and value at %d\n",i, i[ptr_arr]);

#1


4  

You program is leaking memory

你的程序是泄漏内存

unsigned int * ptr_arr = (unsigned int *)malloc(sizeof(unsigned int));

unsigned int * ptr_arr =(unsigned int *)malloc(sizeof(unsigned int));

There is no need to allocate memory for ptr_arr.

不需要为ptr_arr分配内存。

assignment of address of arr1 is enough

arr1的地址分配就足够了

ptr_arr = arr1;

ptr_arr = arr1;

Another way to use it is increment the pointer every time and use it

另一种使用它的方法是每次递增指针并使用它

    ptr_array2 = arr1
    for (int i = 0; i < 8 ; i++)
    {
       printf ("%d", *ptr_array2);
       ptr_array2++;
     }

#2


1  

Both ways of accessing an element in the array are valid. But for readability sake, use array[subscript] instead of *(arrayStart+index).

访问数组中元素的两种方式都是有效的。但为了便于阅读,请使用array [subscript]而不是*(arrayStart + index)。

Example:

int array[10] = {1,2,3,4,5,6,7,8,9,0};

In an array, the elements are contiguously placed, beginning at array(say address 0x00000000). Assuming a 32 bit integer size, 2 is placed at address of array + 4, which is at address 0x00000004, similarly the next element 3 is at address array + 8, which is address 0x00000008 and so on.

在数组中,元素是连续放置的,从数组开始(比如地址0x00000000)。假设32位整数大小,2位于数组+4的地址,位于地址0x00000004,类似地,下一个元素3位于地址数组+8,即地址0x00000008,依此类推。

When I write array[2] I mean I want the second element after the element at array(or array[0]) which is actually the 3rd element in the array. Hence, array indexes start at 0.

当我写数组[2]时,我的意思是我想要在数组(或数组[0])之后的第二个元素,它实际上是数组中的第3个元素。因此,数组索引从0开始。

It literally means, array[0] = *(array + 0), array[1] = *(array + 1), array[2]=*(array+2)

字面意思是,array [0] = *(array + 0),array [1] = *(array + 1),array [2] = *(array + 2)

#3


0  

You need not allocate memory for ptr_arr as you are anyway assigning the address of arr to ptr_arr. Your above code will lead to a memory leak.

您无需为ptr_arr分配内存,因为您无论如何都要将arr的地址分配给ptr_arr。您的上述代码将导致内存泄漏。

You can refer to one of numerous tutorials available in the internet. Just for reference I am posting a few links:

您可以参考互联网上提供的众多教程之一。仅供参考,我发布了一些链接:

http://www.clear.rice.edu/comp221/html/pdf/03-arrays-pointers.pdf http://www.cs.utexas.edu/users/fussell/courses/cs310h/lectures/Lecture_17-310h.pdf

#4


0  

You missed one method:

你错过了一种方法:

     printf("Value of i %d and value at %d\n",i, i[ptr_arr]);