0

I'm currently going over pointers and to further my understanding I'm trying to concatenate two numerical arrays into one using pointers. The code can be seen below.

#include <stdio.h>

void concat(int **pa,int **pb,int **pc)
{
  pc[0]=*pb;
  pc[3]=*pa;
}

int main()
{   int i, array_a[3]={2,4,6},array_b[3]={1,3,5},array_c[6];
    int *p_a=array_a;
    int *p_b=array_b;
    int *p_c=array_c;

    concat(&p_a,&p_b,&p_c);

    for(i=0;i<6;i++)
    {
        printf("array_c[%d]=%d\n",i,p_c[i]);
    }

    return 0;
}

And here is the output.

array_c[0]=1
array_c[1]=3
array_c[2]=5
array_c[3]=0
array_c[4]=2
array_c[5]=4

Press any key to continue.

So it seems to work for the first operation in the function, however the second operation does not fully work. I would've thought this would work. I'm slightly confused with this simple task.

I have done the concatenation using pointers and for loops earlier today and it worked, but I thought I would try using this method using the call by reference and double pointers.

Any help would be greatly appreciated, thanks.

7
  • u can't concatenate objects on stack (your pointers are pointing to objects on stack)... dynamically allocate them (with malloc) then do concatenating Commented Jun 26, 2017 at 21:08
  • what second operation? Commented Jun 26, 2017 at 21:08
  • Sorry I meant this "pc[3]=*pa; " Commented Jun 26, 2017 at 21:09
  • 1
    @InYourDreams The program has undefined behavior. Commented Jun 26, 2017 at 21:10
  • concat completely wrong. YOu are going to have to copy value around, all your function does is move pointers around Commented Jun 26, 2017 at 21:12

2 Answers 2

3

The program has undefined behavior.

Inside the function concat after this statement

pc[0]=*pb;

the original pointer p_c starts to point to the first element of the array array_b.

In fact this statement

pc[0]=*pb;

has the same effect if in main you write

p_c = array_b;

So the pointer p_c now does not point to the array array_c.

This statement in the function

pc[3]=*pa;

results in undefined behavior of the program. because the pointer pc does not point to an array. It points to the single object p_c declared in main.

There is no need to pass pointers to the pointers because the pointers themselves are not changed inside the function.

What you need is to copy elements of two arrays into one array element by element either using loops or the standard function memcpy.

For example

#include <stdio.h>
#include <string.h>

int * concat( int *a, const int *a1, size_t n1, const int *a2, size_t n2 )
{
    memcpy( a, a1, n1 * sizeof( int ) );
    memcpy( a + n1, a2, n2 * sizeof( int ) );

    return a + n1 + n2;
}

#define N1  3
#define N2  3
#define N3  N1 + N2

int main(void) 
{
    int array_a[N1] = { 2, 4, 6};
    int array_b[N2] = { 1, 3, 5};
    int array_c[N3];

    int *p_a = array_a;
    int *p_b = array_b;
    int *p_c = array_c;

    int *result = concat( p_c, p_b, N2, p_a, N1 );

    for( const int *p = p_c; p != result; ++p )
    {
        printf( "%d ", *p );
    }

    putchar( '\n' );

    return 0;
}

The program output is

1 3 5 2 4 6 

Or instead of calling the function memcpy you could use explicitly loops to copy elements of the arrays.

Sign up to request clarification or add additional context in comments.

Comments

0

It seems that there are some basic misunderstandings concerning arrays of ints, pointers to ints, and pointers to pointers to ints on the one hand, and on what a single assignment with an array subscription means.

First, type int **p denotes a pointer to a pointer to an integer, such that pc[3]=pc[0] (which is the same as pc[3]=*pc) actually copies a pointer value rather than an int value; this may invoke undefined behaviour as the actual content is an integral value, yet a pointer value is expected. The arguments should be of type int * or int [] instead.

Second, a single assignment like pc[3]=pb[0] copies only one element, but not a series of elements. Use a loop instead, and pass the number of elements to be copied as an additional parameter:

void concat(const int pa[],const int pb[], int pc[], size_t numElems)
{
    for (size_t i=0; i<numElems; i++) {
        pc[i]=pb[i];
        pc[i+numElems]=pa[i];
    }
}

int main()
{   int i;
    const int array_a[3]={2,4,6}, array_b[3]={1,3,5};
    int array_c[6];

    concat(array_a,array_b,array_c,3);

    for(i=0;i<6;i++) {
        printf("array_c[%d]=%d\n",i,array_c[i]);
    }

    return 0;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.