Error while printing 2D array using pointers - c

I want to print 2D array using pointers but in the printf statement it is showing invalid type argument: (See error in image below:)
#include<stdio.h>
void main()
{
int a[3][3]={1,2,3,4,5,6,7,8,9};
int i,j;
int *p;
p=&a;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
printf("%d",*(p+i)+j);
printf("\n");
}
}
please help me out with this error.

For starters according to the C Standard the function main without parameters shall be declared like
int main( void )
This initializer of the pointer p is incorrect.
int *p;
p = &a;
because the pointer and its initializer have different pointer types and there is no implicit conversion between the types.
This expression
*( *( p + i ) + j)
is also invalid. The expression *( p + i ) yields an object of the type int not of a pointer type.
You can use the following approaches
#include <stdio.h>
int main(void)
{
enum { N = 3 };
int a[N][N] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
int *p = ( int * )a;
for ( size_t i = 0; i < N; i++ )
{
for ( size_t j = 0; j < N; j++ )
{
printf( "%d ", *( p + i * N + j ) );
}
putchar( '\n' );
}
putchar( '\n' );
for ( int ( *p )[N] = a; p != a + N; ++p )
{
for ( int *q = *p; q != *p + N; ++q )
{
printf( "%d ", *q );
}
putchar( '\n' );
}
putchar( '\n' );
return 0;
}
The program output is:
1 2 3
4 5 6
7 8 9
1 2 3
4 5 6
7 8 9

a is a pointer to a pointer but you're assigning it to a pointer. If you fix that, rest should work fine

Related

How is qsort able to take in any array types and sort it?

qsort has the following function protype
void qsort(
void* _Base,
size_t _NumOfElements,
size_t _SizeOfElements,
_CompareFunction
);
How is it possible that it is able to sort any array types (int, double, char, etc.)?
How does qsort know the type of array that I am asking it to sort?
It is due to the comparison function that casts void pointers to required types when it compares elements of the array.
Here is a demonstration program.
#include <stdio.h>
#include <stdlib.h>
int cmp( const void *px, const void *py )
{
int x = *( const int * )px;
int y = *( const int * )py;
return ( y < x ) - ( x < y );
}
int main( void )
{
int a[] ={5, 3, 1, 9, 8, 2, 4, 7};
const size_t N = sizeof( a ) / sizeof( *a );
for (size_t i = 0; i < N; i++ )
{
printf( "%d ", a[i] );
}
putchar( '\n' );
qsort( a, N, sizeof( *a ), cmp );
for (size_t i = 0; i < N; i++ )
{
printf( "%d ", a[i] );
}
putchar( '\n' );
}
The program output is
5 3 1 9 8 2 4 7
1 2 3 4 5 7 8 9
As you can see within the comparison function cmp the passed pointers are casted to the required type.
int x = *( const int * )px;
int y = *( const int * )py;
To traverse the array the function qsort uses the pointer arithmetic the following way. Initially the pointer _Base points to the first element of the passed array. To move for example the pointer to the i-th element of the array it does something like the following
void *ith_element_ptr = ( char * )_Base + i * _SizeOfElements;

main.c:36:9: error: array size missing in ‘num’ int num[]; Why am I getting error for array size although i have given array size in code?

#include <stdio.h>
int removeduplicates(int arr[],int n){
int j=0;
int temp[15];
if(n==0 || n==1){
return n;
}
for(int i=0;i<n-1;i++){
if(arr[i]!=arr[i+1]){
temp[j++]=arr[i];
}
temp[j++]=arr[n-1];
}
for(int i=0;i<j;i++){
arr[i]=temp[i];
}
return j;
}
int main(){
int n;
int num[];
num[]= {1,2,3,3,4,4,5,5,5};
n= sizeof(num)/sizeof(num[0]);
n=removeduplicates(num,n);
printf("%d",n);
return 0;
}
Here in this question I was writing a code to remove duplicates from a sorted array. But I am getting the following error although I defined the array size and although I provided the array size.
main.c:36:9: error: array size missing in ‘num’
int num[];
^~~
main.c:37:9: error: expected expression before ‘]’ token
num[]= {1,2,3,3,4,4,5,5,5};
This code snippet
int num[];
num[]= {1,2,3,3,4,4,5,5,5};
is syntactically incorrect.
Instead write
int num[] = {1,2,3,3,4,4,5,5,5};
Also within the function this declaration with the magic number 15
int temp[15];
and this statement
temp[j++]=arr[n-1];
in the substatement of this for loop
for(int i=0;i<n-1;i++){
if(arr[i]!=arr[i+1]){
temp[j++]=arr[i];
}
temp[j++]=arr[n-1];
}
do not make a sense.
To remove duplicates there is no need to define an auxiliary array.
The function can be written for example the following way as shwon in the demonstrative program below.
#include <stdio.h>
size_t removeduplicates( int arr[], size_t n )
{
size_t m = 0;
for ( size_t i = 0; i != n; )
{
if ( m != i )
{
arr[m] = arr[i];
}
++m;
while ( ( ++i != n && arr[i] == arr[i-1] ) );
}
return m;
}
int main(void)
{
int num[]= { 1, 2, 3, 3, 4, 4, 5, 5, 5 };
const size_t N = sizeof( num ) / sizeof( *num );
for ( size_t i = 0; i != N; i++ )
{
printf( "%d ", num[i] );
}
putchar( '\n' );
size_t m = removeduplicates( num, N );
for ( size_t i = 0; i != m; i++ )
{
printf( "%d ", num[i] );
}
putchar( '\n' );
return 0;
}
The program output is
1 2 3 3 4 4 5 5 5
1 2 3 4 5

What's wrong with this function that reverses arrays using recursivity? ( code and outcome included )

So this is the code I wrote, trying to reverse that array t using recursivity
#include <stdio.h>
#include <stdlib.h>
void rev(int n, float *t)
{
float x;
if(n==0)
{
return 0 ;
}
else
{
x=*t;
*t=*(t+(n-1));
*(t+(n-1))=x;
return rev(n-1, t+1);
}
}
void main()
{
int i;
float t[]={1,2,3,4,5,6,7,8,9};
int n=sizeof t /sizeof *t;
rev (n,t);
for(i=0;i<n;i++) printf("%f",t[i]);
}
I'd like to understand why this solution does not work, I'm not that interested in the solution overall but I want to understand what mistakes I made in this one for it not to work.
For starters a function that has the return type void shall return nothing value. So this statement
return 0 ;
is invalid.
The function swaps two elements so the size of the array must be decremented by 2.
The function can look like
void rev( float a[], size_t n )
{
if ( !( n < 2 ) )
{
float tmp = *a;
*a = *( a + n - 1 );
*( a + n - 1 ) = tmp;
rev( a + 1, n - 2 );
}
}
and be called like
size_t n = sizeof t /sizeof *t;
rev( t, n);
Here is a demonstrative program.
#include <stdio.h>
void rev( float a[], size_t n )
{
if ( !( n < 2 ) )
{
float tmp = *a;
*a = *( a + n - 1 );
*( a + n - 1 ) = tmp;
rev( a + 1, n - 2 );
}
}
int main(void)
{
float a[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
size_t n = sizeof a /sizeof *a;
for ( size_t i = 0; i < n; i++ )
{
printf( "%.0f ", a[i] );
}
putchar( '\n' );
rev( a, n );
for ( size_t i = 0; i < n; i++ )
{
printf( "%.0f ", a[i] );
}
putchar( '\n' );
return 0;
}
The program output is
1 2 3 4 5 6 7 8 9
9 8 7 6 5 4 3 2 1
Pay attention to that according to the C Standard the function main shall be declared like
int main( void )
instead of
void main()
There are small easy to fix problems like return 0; and return rev(n-1, t+1);. The first one should be just
return;
because you can't return anything from a function returning void.
The other should be a call to rev() itself
rev(n-1, t+1);
because that's what recursive functions do (and also because you can't return anything)
Then you should use int main( void ) or at least int main() Difference between int main() and int main(void)?
Finally, you have a logic error here
x=*t;
*t=*(t+(n-1));
*(t+(n-1))=x;
rev(n-1, t+1);
*(t+(n-1)) will always be the value of the last element of the array: yes you pass n-1 so you expect that if *(t+(n-1)) was 8th element, in the next call it will be 7th, however you are also passing t+1 so *(t+(n-1)) will always be the 8th element of the array.
And even this one is an easy-to-fix problem. You just pass n-2.
Here's your recursive function
void rev(int n, float *t)
{
float x;
if(n > 0) {
x=*t;
*t=*(t+(n-1));
*(t+(n-1))=x;
rev(n-2, t+1);
}
return;
}
Doing
if(n != 0) {
.... something....
}
return;
Is the same of doing
if(n == 0) {
return;
}
else {
... something...
}
I put n > 0 instead of n != 0 because since n is always initially positive the two conditions are equivalent, however since you pass n-2 if n is an odd number you are going to have negative values of n without passing for 0.

free() in dynamic memory

Do I use correctly free() in the code below? Is it a memory leak? Is it a problem use free() in the main part and not in the function? If yes there is a method to free in the function and not in main?
This code copy an array in another one.
int *copy(const int *arr,int n);
int main(){
int *p_arr1,*p_arr2;
int n,i;
printf("Insert size of array: ");
scanf("%d",&n);
p_arr1 = calloc(n,sizeof(int));
for(i=0;i<n;i++){
printf("Insert element %d of the array: ",i+1);
scanf("%d",p_arr1+i);
}
p_arr2 = copy(p_arr1,n);
for(i=0;i<n;i++){
printf("%d ",*p_arr2);
p_arr2++;
}
free(p_arr1);
free(p_arr2);
return 0;
}
int *copy(const int *arr,int n){
int i;
int *new;
new = calloc(n, sizeof(int));
for(i=0;i<n;i++){
new[i] += arr[i];
}
return new;
}
As long as you have the pointer returned by malloc (or in your case calloc) you can pass it to free when and wherever you want, it doesn't have to be in the same function.
However, after the loop where you print the contents of p_arr2, you no longer have the pointer returned by calloc inside the function, because you modify the pointer in the loop.
You need to use a temporary pointer variable for the loop:
int *p_arr2_tmp = p_arr2;
for (size_t i = 0; i < n; ++i)
{
printf("%d ", *p_arr2_tmp);
++p_arr2_tmp;
}
// Now we can free the memory pointed to by the original p_arr2 pointer
free(p_arr2);
Or you could use simple array indexing instead:
for (size_t i = 0; i < n; ++i)
{
printf("%d ", p_arr2[i]);
}
// The pointer p_arr2 wasn't modified, so it can be passed to free
free(p_arr2);
In this loop
for(i=0;i<n;i++){
printf("%d ",*p_arr2);
p_arr2++;
}
the value of the pointer p_arr2 is being changed, So using the changed pointer in a call of free results in undefined behavior.
You should write
for(i=0;i<n;i++){
printf("%d ", p_arr2[i] );
}
Also it is unclear why you are using the compound operator += in the function instead of the operator =.
new[i] += arr[i];
The function can be defined the following way
int * copy( const int *arr, size_t n )
{
int *new_arr = malloc( n * sizeof( int ) );
if ( new_arr != NULL )
{
memcpy( new_arr, arr, n * sizeof( int ) );
}
return new_arr;
}
If you want to use a pointer in the loop that outputs the newly created array then it can look the following way
for ( const int *p = p_arr2; p != p_arr2 + n; ++p )
{
printf( "%d ",*p );
}
putchar( '\n' );
If the aim is to write a program that uses only pointers and excludes using of the subscript operator and indices then your program can look the following way
#include <stdio.h>
#include <stdlib.h>
int * copy( const int *arr, size_t n )
{
int *new_arr = malloc( n * sizeof( int ) );
if ( new_arr != NULL )
{
for ( int *p = new_arr; p != new_arr + n; ++p )
{
*p = *arr++;
}
}
return new_arr;
}
int main(void)
{
size_t n;
printf( "Insert size of array: " );
scanf( "%zu", &n );
int *p_arr1 = calloc( n, sizeof( int ) );
for ( int *p = p_arr1; p != p_arr1 + n; ++p )
{
printf( "Insert element %d of the array: ", ( int )( p - p_arr1 + 1 ) );
scanf( "%d", p );
}
int *p_arr2 = copy( p_arr1, n );
if ( p_arr2 != NULL )
{
for ( const int *p = p_arr2; p != p_arr2 + n; ++p )
{
printf( "%d ",*p );
}
putchar( '\n' );
}
free( p_arr2 );
free( p_arr1 );
return 0;
}
The program output might look like
Insert size of array: 10
Insert element 1 of the array: 0
Insert element 2 of the array: 1
Insert element 3 of the array: 2
Insert element 4 of the array: 3
Insert element 5 of the array: 4
Insert element 6 of the array: 5
Insert element 7 of the array: 6
Insert element 8 of the array: 7
Insert element 9 of the array: 8
Insert element 10 of the array: 9
0 1 2 3 4 5 6 7 8 9

Copying Array Using Pointers

I am trying to create a copy of an array by only accessing an array using pointer arithmetic. This is my function
int* arrCopy(int *a, int size){
int *b = (int*)malloc(size*sizeof(int));
for(int i = 0; i < size; i++){
*(b+(sizeof(int)*i)) = *(a+(sizeof(int)*i));
}
return b;
}
When I print the array it shows that the copied array is filled with the repeating of the first two values in the original array. So if the original array was [1, 2, 3, 4, 5], then arrcopy would be [1, 2, 1 ,2 ,1] and I can't figure out why.
Here you are
int * arrCopy( const int *a, size_t n )
{
int *b = malloc( n * sizeof( int ) );
for ( size_t i = 0; i < n; i++ )
{
*( b + i ) = *( a + i );
}
return b;
}
Or you could check whether the memory was successfully allocated.
int * arrCopy( const int *a, size_t n )
{
int *b = malloc( n * sizeof( int ) );
if ( b != NULL )
{
for ( size_t i = 0; i < n; i++ )
{
*( b + i ) = *( a + i );
}
}
return b;
}
Here is a demonstrative program
#include <stdio.h>
#include <stdlib.h>
int * arrCopy( const int *a, size_t n )
{
int *b = malloc( n * sizeof( int ) );
if ( b != NULL )
{
for ( size_t i = 0; i < n; i++ )
{
*( b + i ) = *( a + i );
}
}
return b;
}
int main(void)
{
int a[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
const size_t N = sizeof( a ) / sizeof( *a );
int *b = arrCopy( a, N );
if ( b != NULL )
{
for ( size_t i = 0; i < N; i++ )
{
printf( "%d ", b[i] );
}
putchar( '\n' );
}
free( b );
return 0;
}
Its output is
0 1 2 3 4 5 6 7 8 9
As for your code then for example this expression
*(b+(sizeof(int)*i))
is invalid. It selects the element of the array b equivalent to
b[sizeof(int)*i]
So for example when i is equal to 1 and sizeof( int ) equal to 4 then instead of getting the element b[1] you are getting the element b[4].
From the C Standard (6.5.2.1 Array subscripting)
2 A postfix expression followed by an expression in square brackets []
is a subscripted designation of an element of an array object. The
definition of the subscript operator [] is that E1[E2] is identical to
(*((E1)+(E2))). Because of the conversion rules that apply to the
binary + operator, if E1 is an array object (equivalently, a pointer
to the initial element of an array object) and E2 is an integer,
E1[E2] designates the E2-th element of E1 (counting from zero).

Resources