Click here to Skip to main content
16,016,580 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Following is my code. Actually I'm trying to reverse an array. Though I can easily do that using actual arrays i.e. "int arr[]" but I thought why not try something new, Pointers :-) but now I'm not able to figure out where the problem is.

I'm new to C and have some prior very basic knowledge in coding.
C++
int i, *arr, num, numImg, temp;
printf("Enter num: ");
scanf("%d", &num);

numImg = num - 1;

arr = (int*)malloc(num * sizeof(int));

for(i = 0; i < num; i++)
{
    scanf("%d", arr + i);
}


for(i = 0; i < num; i++)
{
    temp = *(arr + numImg);
    *(arr + numImg) = *(arr + i);
    *(arr + i) = temp;
    numImg--;
    printf("%d, ", *(arr + i));
}


And I have one more question. Not sure if need to create a new thread or not but what I want to know is "How to configure KDevelop for debugging?" So far only managed to get the error "&"warning: GDB: Failed to set controlling terminal: Operation not permitted\n"
Enter num:
"


What I have tried:

Tried to check if what I'm tryin to achieve is even possible and written the following code and it worked but the above code didn't
C++
int n, m, *ptrN, *ptrM;

n = 10;
ptrN = &n;

m = 50;
ptrM = &m;

*ptrN = *ptrM;

printf("%d", *ptrN);

OUTPUT =--> 50
Posted
Updated 10-Oct-22 23:42pm
v2
Comments
Christian Graus 8-May-19 0:24am    
Also, everyone wants to know what's wrong with their code. A descriptive title will attract people using what you're using. Especially useful with a language that's on the fringes like C

Quote:
Actually I'm trying to reverse an array.

You reverse the array by swapping elements 2 by 2, but you have to stop when you reach the middle of array.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 
Comments
Mohit Tomar 8-May-19 12:22pm    
Yes, correct I needed to stop at the middle of the array and should have used a separate array for printing the elements of the array.

I used the variable now as a < n/2 and it is working now.

Thanks again
int i, *arr, num, numImg, temp;
printf ( "Enter num: " );
scanf ( "%d", &num );
numImg = num - 1;

arr = ( int* ) malloc ( num * sizeof ( int ) );

for ( i = 0; i < num; i++ ) {
    scanf ( "%d", arr + i );
}

for ( i = 0; i < num / 2; i++ ) {
    temp = * ( arr + numImg );
    * ( arr + numImg ) = * ( arr + i );
    * ( arr + i ) = temp;
    numImg--;
    //printf("%d, ", *(arr + i));
}

for ( i = 0; i < num; i++ ) {
    printf ( "%d ", * ( arr + i ) );
}


I'm posting the final solution to the problem above, in case if someone else needed it. Thanks
 
Share this answer
 
Why on earth are you learning C?

You reference an array item like this:

arr[numImg]


If it was a char *, this code would work as a char is one byte. As it is, you're ending up at random points in your data.
 
Share this answer
 
Comments
Stefan_Lang 8-May-19 3:17am    
Actually his code works with any type. In C and C++, either of the following gets you the same result:
arr[numImg]
*(arr+numImg)
*(numImg+arr)
numImg[arr]
0[numImg+arr]

In all of these cases, arr and numImg are added, and then interpreted and dereferenced as a pointer to int (or whatever type they are pointing to).

P.S.:
Try this code at https://www.tutorialspoint.com/compile_c_online.php :
#include <stdio.h>

int main()
{
int arr [] = { 3,4,5 };
int i = 2;
printf("arr[i]: %d\n", arr[i]);
printf("*(arr+i): %d\n", *(arr+i));
printf("*(i+arr): %d\n", *(i+arr));
printf("i[arr]: %d\n", i[arr]);
printf("0[i+arr]: %d\n", 0[i+arr]);

return 0;
}
Mohit Tomar 8-May-19 12:23pm    
I have plenty of time that's why.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900