Accessing a C char array from a pointer

I am trying to pass a C char array to a function via its pointer and then accessing the char array in the function. At the moment I can only seem to access the first character and the rest is just random bytes. I have been trying to check the variables before and after the… Read More Accessing a C char array from a pointer

A warning I don't understand using malloc() in C language

This is some code I wrote to exercise using malloc(). In particular, I want to have a char-array of dimension and elements given by the user and to print these elements. #include <stdio.h> #include <stdlib.h> int main() { char *ptr; int d; printf("Inserire la dimensione dell’array CHAR:\n"); scanf("%d", &d); ptr = (char *) malloc(d *… Read More A warning I don't understand using malloc() in C language

How to return a Pointer-To-Array from a function in C?

Pointers-to-array (not array pointers) are a lesser-known feature of the C programming language. int arr[] = { 3, 5, 6, 7, 9 }; int (*arr_ptr)[5] = &arr; printf("[2]=%d", (*arr_ptr)[2]); They allow you to "un-decay" a dynamically allocated array pointer. (which is pretty cool in my opinion) int *ptr = malloc(sizeof(int) * 5); int(*arr_ptr)[5] = (int(*)[5])ptr;… Read More How to return a Pointer-To-Array from a function in C?