Print Array Pointer value in Reverse Order - c

I want to Print Pointer Array value in Reverse
#include <stdio.h>
#define size 5
int main()
{
int a[size] = {1,2,3,4,5};
int i;
int *pa = a;
for(i = size; i >0; i--)
{
printf("a[%d] = %d\n",i,*pa);
pa++;
}
return 0;
}
Output:
a[5] = 1
a[4] = 2
a[3] = 3
a[2] = 4
a[1] = 5
The output I want is:
a[5] = 5
a[4] = 4
a[3] = 3
a[2] = 2
a[1] = 1

replace with this
#include <stdio.h>
#define size 5
int main()
{
int a[size] = {1,2,3,4,5};
int i;
int *pa = (a+size-1);
for(i = size; i >0; i--)
{
printf("a[%d] = %d\n",i,*pa);
pa--;
}
return 0;
}

You're making this too hard. Given a pointer into an array, you can use the indexing operator on it just as you would on the array itself:
int a[size] = {1,2,3,4,5};
int i;
int *pa = a;
for (i = size - 1; i >= 0; i--) {
printf("a[%d] = %d\n", i, pa[i]);
}
Alternatively, if you want to avoid the indexing operator for some reason, then just start your pointer at one past the end ...
*pa = a + size;
... and decrement it as you proceed through the loop:
for (i = size - 1; i >= 0; i--) {
pa--;
printf("a[%d] = %d\n", i, *pa);
}
Do note, by the way, that array indexing in C starts at 0, as the example codes above properly account for.

You don't have to use pointer. A simpler implementation
#include <stdio.h>
#define size 5
int main()
{
int a[size] = {1,2,3,4,5};
for(int i = size-1; i >=0; i--)
printf("a[%d] = %d\n",i,a[i]);
return 0;
}

Related

Function allocate and modify many (more than one) arrays in C [duplicate]

This question already has answers here:
Changing address contained by pointer using function
(5 answers)
Closed 2 years ago.
A C function can modify more than one variable by an illusion of pass-by-reference (a pass-by-value of address as explained by Ely), e.g.:
#include <stdio.h>
void function(int *pa, int *pb) {
*pa *= *pa;
*pb *= *pb;
}
int main(void) {
int a = 1, b = 2;
function(&a, &b);
printf("a = %d\nb = %d\n", a, b);
return 0;
}
which outputs
a = 1
b = 4
It is also possible to modify a whole range of variables by returning a pointer to an array, e.g.:
#include <stdlib.h>
#include <stdio.h>
int *function(int *ptr_size) {
int n = 6; // arbitrary ptr_size
int *array = (int *)malloc(n * sizeof(int));
for (int i = 0; i < n; ++i)
array[i] = i * i;
//
*ptr_size = n;
return array;
}
int main(void) {
int size = 0;
int *array = function(&size);
printf("size = %d\n", size);
for (int i = 0; i < size; ++i)
printf("array[%d] = %d\n", i, array[i]);
free(array);
array = NULL;
return 0;
}
which outputs :
size = 6
array[0] = 0
array[1] = 1
array[2] = 4
array[3] = 9
array[4] = 16
array[5] = 25
But what if I want a function that modify more than one (dynamic allocated) array ?
I tried this
#include <stdlib.h>
#include <stdio.h>
void function(int *array, int *ptr_asize, int *brray, int *ptr_bsize) {
int size = 6;
array = (int *)malloc(size * sizeof(int));
brray = (int *)malloc(size * sizeof(int));
for (int i = 0; i < size; ++i) {
array[i] = i * i;
brray[i] = i * i * i;
}
*ptr_asize = size;
*ptr_bsize = size;
}
int main(void) {
int asize, bsize;
int *array, *brray;
function(array, &asize, brray, &bsize);
// array
printf("asize = %d\n", asize);
for (int i = 0; i < asize; ++i)
printf("array[%d] = %d\n", i, array[i]);
free(array);
array = NULL;
// brray
printf("bsize = %d\n", bsize);
for (int i = 0; i < bsize; ++i)
printf("brray[%d] = %d\n", i, brray[i]);
free(brray);
brray = NULL;
//
return 0;
}
but it makes a segmentation fault.
That is not very surprising, how main would know about how much memory has been allocated to array and brray?
So my question is: is it possible in C that a function allocate and modify more than one array, and those changes remain in main?
PS: A solution would be to allocate a new abrray that contains both array and brray (int **function(...) { ... return abrray; }), but I would like to know if it is possible to a function to modify two (or more) arrays, and that changes remain in main.
Pass a pointer to pointer, like this:
void func(int** array, int** brray, size_t size_a, size_t size_b) {
*array = malloc(size_a * sizeof(int));
*brray = malloc(size_b * sizeof(int));
}
Call it like:
int *arr, *brr;
func(&arr, &brr, 2, 5);
As explained by iTs.SL4y3r, it is possible by passing pointer to pointer.
Here is a minimal working example :
#include <stdlib.h>
#include <stdio.h>
void function(int **array, int *ptr_asize, int **brray, int *ptr_bsize) {
int size = 6;
*array = malloc(size * sizeof(int));
*brray = malloc(size * sizeof(int));
for (int i = 0; i < size; ++i) {
(*array)[i] = i * i;
(*brray)[i] = i * i * i;
}
*ptr_asize = size;
*ptr_bsize = size;
}
int main() {
int asize, bsize;
int *array, *brray;
function(&array, &asize, &brray, &bsize);
// array
printf("asize = %d\n", asize);
for (int i = 0; i < asize; ++i)
printf("array[%d] = %d\n", i, array[i]);
free(array);
array = NULL;
// brray
printf("bsize = %d\n", bsize);
for (int i = 0; i < bsize; ++i)
printf("brray[%d] = %d\n", i, brray[i]);
free(brray);
brray = NULL;
//
return 0;
}
which outputs :
asize = 6
array[0] = 0
array[1] = 1
array[2] = 4
array[3] = 9
array[4] = 16
array[5] = 25
bsize = 6
brray[0] = 0
brray[1] = 1
brray[2] = 8
brray[3] = 27
brray[4] = 64
brray[5] = 125

How to jumble up the order of a pointer to an array of strings in C?

Hi i was given this question and i dont know how to proceed with my attempt.
"Considering the 12 words below. Write a C Program which has functions to do the following:
Jumble the order of words
"Computer" "Science" "is" "my" "favourite" "topic" "and" "I" "learn" "many" "interesting" "things""
my answer failed and i was wondering if you guys could finish my approach or give a better approach do doing so.
#include <stdio.h>
void randomise();
int main(void){
const char *a[12];
a[0] = "Computer";
a[1] = "Science";
a[2] = "is";
a[3] = "my";
a[4] = "favourite";
a[5] = "topic";
a[6] = "and";
a[7] = "I";
a[8] = "learn";
a[9] = "many";
a[10] = "interesting";
a[11] = "things";
randomise();
}
void randomise(){
int i;
int lower = 0, upper = 12;
for (i = 0; i < 12; i++) {
int num = rand() % 12;
printf("%d \n", *a[num]);
}
}
I have cleaned it alittle bit up for you.
You do not have an access to the a array as it is "local" to the main function. You need to pass it to the function and inform how many elements this array has.
#include <stdlib.h>
#include <stdio.h>
void randomise(char **ptr, size_t num);
int main(void)
{
char *a[12];
a[0] = "Computer";
a[1] = "Science";
a[2] = "is";
a[3] = "my";
a[4] = "favourite";
a[5] = "topic";
a[6] = "and";
a[7] = "I";
a[8] = "learn";
a[9] = "many";
a[10] = "interesting";
a[11] = "things";
randomise(a, 12);
}
void randomise(char **ptr, size_t num)
{
int i;
for (i = 0; i < num; i++)
{
printf("%s \n", ptr[rand() % num]);
}
}

My code doesn't returns an output. It returns only exit code

I wanted to reverse half of the arrays inputs with the other half.
#include <stdio.h>
#include <math.h>
void main() {
double size = 5;
int array[5] = {1, 2, 3, 4, 5};
int half_size = ceil(size / 2);
for(int i = 0; i < half_size; i++){
int a;
int rev = size - (i + 1);
array[i] = a;
array[i] = array[rev];
array[rev] = a;`enter code here`
}
printf("%d", array[5]);
}
I agree with #Eugene Sh.'s and #FredK's suggestions. The line array[5] in the line printf("%d", array[5]); is out of bound since array only have indexes from 0 to 4. Since I assume you want to print out the last element in the array, you should change it to printf("%d", array[4]);. Another thing is that your assignment expression array[i] = a; is wrong. I assume the expression is part of the swapping process from element in index i with element in index rev. If that was the case then you should change it to a = array[i]; instead. I update you code according to my suggestion and it outputs the correct result. I added the for loop to verify that the array values are reversed for testing purpose. You can delete it after you're done testing.
#include <math.h>
int main() {
double size = 5;
int array[5] = {1, 2, 3, 4, 5};
int half_size = ceil(size / 2);
for(int i = 0; i < half_size; i++){
int a;
int rev = size - (i + 1);
a = array[i];
array[i] = array[rev];
array[rev] = a;
}
for (int i = 0; i < size; ++i) {
printf("%d ", array[i]);
}
printf("\n");
printf("%d", array[4]);
}

Sum of two arrays using pointers in C

I am trying to create a program in C that calculates the sum of two int arrays using pointers.
Here is an example of what I want to do:
int a[] = {1,2,3,4}
int b[] = {1,2,3,4,5,6}
int c[] = sumArrays(a,b,4,6)
Output : c = {2,4,6,8,5,6}
The problem is my output is different, it shows:
Output : c = {2,4,6,8}
Any idea what did I do wrong and how to correct it ?
Here is my code :
#include <stdio.h>
#include <stdlib.h>
int* sumArrays(int *arr1, int *arr2, int dim1,int dim2)
{
int *ret = NULL;
if(dim1<dim2) ret = (int*) malloc(dim2*sizeof(int));
else ret = (int*) malloc(dim1*sizeof(int));
if(ret)
{
if(dim1<dim2) {
int i = 0;
for (i = 0;i < dim1;i++) {
ret[i] = arr1[i] + arr2[i];
}
for (i = dim1; i < dim2;i++) {
ret[i]=arr2[i];
}
} else {
int i = 0;
for (i = 0;i < dim2;i++) {
ret[i] = arr1[i] + arr2[i];
}
for (i = dim2; i < dim1;i++) {
ret[i]=arr1[i];
}
}
}
return ret;
}
int main()
{
int *a[] = {1,2,3,4};
int *b[] = {1,2,3,4,5,6};
int *c = sumArrays(a,b,4,6);
printf("c = ");
int i;
for (i = 0; i < sizeof(c); i++) {
printf("%d,",c[i]);
}
}
Sizeof c will always return 4 for 32-bit system and 8 for 64-bit, because c is a pointer to int.
So to print the result array you should write:
for (i = 0; i < 6; i++) {
You have problem about using pointers.
int *a[] = {1,2,3,4}; // a is an array of pointers to integers.
int *b[] = {1,2,3,4,5,6}; // b is an array of pointers to integers.
By doing this, you are declaring array of pointers to integers. So, it most likely causes compiler warning or error depending on your compile settings like, initialization makes pointer from integer without a cast. When you are passing the actual arguments to formal parameters, it causes same warning as well. Your main should be like this,
int main()
{
int a[] = {1,2,3,4};
int b[] = {1,2,3,4,5,6};
int sizeA = sizeof(a) / sizeof(*a);
int sizeB = sizeof(b) / sizeof(*b);
int *c = sumArrays(a,b,sizeA,sizeB);
printf("c = ");
int i;
for (i = 0; i < (sizeA < sizeB ? sizeB : sizeA); i++) {
printf("%d,",c[i]);
}
}

C seg faults when assigning an array through function and printing the array

I feel like two code snippets are doing the same thing, yet one seg faults
code 1: this prints fine
int main(){
int n = 3;
int i;
int *arr = (int *) malloc(sizeof(int) * n);
int * can_arr;
arr[0] = 3;
arr[1] = 2;
arr[2] = 1;
arr[3] = 2;
can_arr = arr;
for(i = 0; i <= n; i++)
printf("%d ", can_arr[i]);
return 0;
}
code 2: this seg faults when I print
void get_arr(int n, int *arr, int *can_arr){
can_arr = arr;
}
int main(){
int n = 3;
int i;
int *arr = (int *) malloc(sizeof(int) * n);
int * can_arr;
arr[0] = 3;
arr[1] = 2;
arr[2] = 1;
arr[3] = 2;
get_arr(n, arr, can_arr);
for(i = 0; i <= n; i++)
printf("%d ", can_arr[i]);
return 0;
}
why? both are doing can_arr = arr? I am doing something else in my code that requires this array assignment. I have just simplified it here. So why does it seg fault?
The problem is that can_arr from main is passed to the function get_arr by value. This means that the line:
can_arr = arr;
in that function has the effect that the local variable can_arr is assigned a value. But that variable is local to the function. So it will not affect the can_arr variable in main. So when you try to read from can_arr at the loop, can_arr will still be uninitialized.
The array arr pointer in both examples has been allocated enough memory for 3 elements, with
int n = 3;
int *arr = (int *) malloc(sizeof(int) * n);
// ...
arr[3] = 2;
But you are indexing a 4th element arr[3]. So whatever appears to work, is by luck.
Also the loop
for(i = 0; i <= n; i++)
is incorrect it should be
for(i = 0; i < n; i++)

Resources