简体   繁体   中英

How can I copy value in a struct that is passed by reference to the function and member A is also a pointer?

I am learning the pointer and I stuck on an issue as I cannot debug the program because it gives me address rather than the actual array.

I am trying to create an Abstract datatype of Array

struct ARRAY_ADT
{
    int *A; // For storing the pointer to base address of the array
    int size; //For storing the size of the array
    int len; //For storing the length of the array
}; 

Now to insert a number in it's correct index I had written a function called Insert . I am calling it from main.

It's look like this

void insert(struct ARRAY_ADT *Array)
{
    int num;
    int index;
    int i = Array -> len - 1;

    printf("Enter the value to be inserted: ");
    scanf("%d", &num);

    printf("Enter the value to be index: ");
    scanf("%d", &index);

    printf("Array length:%d \n", Array ->len);


    if(OutofRange(Array -> size, Array -> len))
    {
        printf("Array is full:");
    }
    else
    {
        if (OutofRange(Array -> size, index))
        {
            printf("Index is not valid");
        }
        else
        {
            //int *temp = NULL;

            while(i >= index)
            {
                Array->(A+i+1) = Array->(A+i); // Problem
                i--;
            }
           
            Array -> A[index] = num;
            Array -> len++;
        }

    }
}

So, how can I copy value from right to left to make place for the new number.

How can I copy value in a struct that is passed by reference to the function and member A is also a pointer?

I just want to perform this operation

Array.A[i++] = Array.A[i];

Use Array->A[i+1] = Array->A[i] . Or you can shift without a loop by using memmove()

memmove(&(Array->A[i+1]), &(Array->A[i]), sizeof(Array->A[i]) * (Array->len - index));

right is

struct xntype
{
  int *b;
} xn;
*(xn.b + 1) = 15;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM