I am trying to create an array and assign values to it. While I am able to assign values correctly, I am unable to retrieve those values and I have no idea why.
Here is a part of the code snippet.
void *arr = (void *)(malloc(sizeof(void *) * 5));
int i = 0;
for(i = 0; i < 5; i++) {
*(int *)(arr+i) = initial[i];
printf("Value of array: %d\n", *(int *)(arr+i));
}
for(i = 0; i < 5; i++) {
printf("Pushing: %d\n", *(int *)(arr+i));
DArray_push(result, (arr+i));
}
The output of the program is
Value of array: 4
Value of array: 1
Value of array: 3
Value of array: 2
Value of array: 0
Pushing: 33751300
Pushing: 131841
Pushing: 515
Pushing: 2
Pushing: 0
Why is my program able to accept values correctly, but is printing out garbage values when I try to retrieve them later?
Turn warnings on. (arr+i) is not defined (you cannot add to a void pointer and you do the cast only after this addition). You see undefined behavior.
You further allocate things the size of a pointer but treat it as int (more undefined behavior).
Why not write:
int *arr = malloc(sizeof(int) * 5);
int i = 0;
for(i = 0; i < 5; i++) {
arr[i] = initial[i];
printf("Value of array: %d\n", arr[i]);
}
for(i = 0; i < 5; i++) {
printf("Pushing: %d\n", arr[i]);
}
Pushing: 33751300 = 4 + 1 * 256 + 3 * 256 * 256 + 2 * 256 * 256 * 256
Pushing: 131841 = 1 + 3 * 256 + 2 * 256 * 256
Pushing: 515 = 3 + 2 * 256
Pushing: 2
Pushing: 0
That part is already wrong, but would on most 32 or 64 bit compilers still work: sizeof(void *). Only because this this at least the size of an int on these platforms though.
*(int *)(arr+i)
This is the actual problem. This did not access the next integer, it accessed the next address addressable by a void pointer, which is typically a single byte. Accessing the next integer would had been equivalent to this:
*(int *)(arr + i*sizeof(int))
Or easier to read:
*( ((int *)arr) + i)
Remember, incrementing a pointer by an offset doesn't add to the raw memory address, but it depends on the pointer type.
The point here is the arithmetic of pointers. When we're adding a value by 1, it wouldn't be the same for different type pointers. So let's check an example:
(int *)p + 1 // it's moving 4 bytes
(void *)p + 1 // it's moving 1 byte in my compiler, void * is a generic type
In your example we know we're working with integers, so we need to tell the compiler to convert the generic pointer to the appropriate type before we do any operations with.
Try to execute the following code to understand what I'm gonna talking about:
#include <stdio.h>
#include <stdlib.h>
void main() {
int initial[] = {100, 101, 102, 103, 104};
void *arr = (void *)(malloc(sizeof(void *) * 5));
int i = 0;
for (i = 0; i < 5; i++) {
printf("%p\t%p\n", arr + i, ((int *)arr) + i);
}
for(i = 0; i < 5; i++) {
*(((int *)arr)+i) = initial[i];
printf("Value of array: %d\n", *(((int *)arr)+i));
}
for(i = 0; i < 5; i++) {
printf("Pushing: %d\n", *(((int *)arr)+i));
}
}
I found some resources to help:
https://www.cs.umd.edu/class/sum2003/cmsc311/Notes/BitOp/pointer.html
https://www.viva64.com/en/t/0005/
The thing is that you don't reset your arr pointer. Thus in your second loop you actually print the values after the initial array.
You need to create a variable to traverse your array.
void *p = arr;
And then use it to traverse the array. And again before the second loop.
Moreover, do not cast the return value of a malloc function call (even when you are casting it to a void pointer which is the actual return type of malloc).
EDIT: you actually don't change the value of arr. My bad.
First of all, the thing with casting malloc to void and using sizeof void is totally pointeless.
Assuming initial[]is another array, the fix would be:
#include <stdio.h>
#include <stdlib.h>
int initial[] = {10, 20, 30, 40, 50};
int main()
{
//allocate memory for 5 integers
int* arr = malloc(sizeof(int) * 5);
//loop over and copy from inital to arr
int i = 0;
for(i = 0; i < 5; i++) {
arr[i] = initial[i];
printf("Value of array: %d\n", arr[i]);
}
//pointer to first element of arr
int* arrPointer = arr;
for(i = 0; i < 5; i++) {
printf("Pushing: %d\n", *arrPointer);
//DArray_push(result, (*arrPointer));
//increment the arrPointer
arrPointer++;
}
//reset pointer if needed later...
arrPointer = arr;
return 0;
}
Related
I've a problem when I run the program which is:
invalid operands to binary - (have 'int' and 'int *')
Since I defined a dynamic array **engelCikacagiYer then I tried to make an operation on karakterHP
int karakterHP = 100;
int **engelCikacagiYer;
engelCikacagiYer = malloc(engelSayisi * sizeof(int));
for(i = 0; i < engelSayisi; i++){
engelCikacagiYer[i] = (rand() % (100 - 5 + 1) + 5);
}
for(i = parkurUzunlugu.Baslangic; i < parkurUzunlugu.Bitis; i++){
if (asal(engelCikacagiYer)) {
karakterHP -= engelCikacagiYer[i];
printf("%d\t", karakterHP);
}
}
This piece of code:
int **engelCikacagiYer;
engelCikacagiYer = malloc(engelSayisi*sizeof(int));
Is not correct, to allocate memory to a double pointer, aka pointer to pointer you need to allocate using the size it points to and that is pointer to int so you would need:
engelCikacagiYer = malloc(engelSayisi*sizeof(int*));
Or better yet:
engelCikacagiYer = malloc(engelSayisi*sizeof(*engelCikacagiYer));
Later you try to assign an int value to a engelCikacagiYer[i] which is a pointer variable not an int variable, hence the error.
If you really need a double pointer, after the first malloc you will need to allocate memory for each engelCikacagiYer[i] pointer:
for(int i = 0; i < engelSayisi; i++){
engelCikacagiYer[i] = malloc(sizeof(**engelCikacagiYer));
}
And assing values as if it was a 2D array (I mean using the same notation), example:
engelCikacagiYer[i][0] = (rand()%(100-5+1)+5);
or
*engelCikacagiYer[i] = (rand()%(100-5+1)+5);
Here is a simplified possible implementation of a correct allocation:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int **engelCikacagiYer;
engelCikacagiYer = malloc(100 * sizeof(*engelCikacagiYer)); //check allocation for errors
for(int i = 0; i < 100; i++){
engelCikacagiYer[i] = malloc(sizeof(**engelCikacagiYer)); //check allocation
*engelCikacagiYer[i] = (rand() % (100 - 5 + 1) + 5);
}
for (int i = 0; i < 100; i++) //test print
{
printf("%d ", *engelCikacagiYer[i]);
}
}
In any case, for the code you present using a single pointer to int would also be a valid (and simpler) solution.
Change int **engelCikacagiYer; to int *engelCikacagiYer;.
By your code it seems you want a dynamic array of ints, not a dynamic array of int pointers.
I am new to C programming and especially to pointers. In the program I wrote, I tried to write a function that returns a pointer to specified column of array. See the code below for better understanding (or confusion :) ):
#include <stdio.h>
#include <stdlib.h>
// function for getting pointer to specidifed column index
// 'ind' is index of requested column, 'ncol' is number of items in column
int* get_col(const int* arr, unsigned int ind, unsigned int ncol);
int main() {
unsigned int n;
printf("Input matrix size : ");
scanf("%i", &n);
int arr[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++)
arr[i][j] = i * n + j;
}
for (int i = 0; i < n; i++) {
printf("values in column %d: \n", i);
int *col = get_col((int*)arr, i, n);
for (int j = 0; j < n; j++) {
printf("%d ", *col);
col = col + 1;
}
printf("\n");
}
return 0;
}
int* get_col(const int* arr, unsigned int ind, unsigned int ncol) {
int *result = malloc(sizeof(int) * ncol);
for (int i = 0; i < ncol; i++)
*result = *(arr + i*ncol + ind);
return result;
}
As you see get_col function accepts pointer to array, column index and column size (n of elements in column, i.e number of rows) as arguments and trying to return a pointer to 1D array that contains values of column at requested index. The problem is that result is not correct. In case n=3 results are like below:
Input matrix size : 3
values in column 0:
6 0 0 // supposed to be 0 3 6
values in column 1:
7 0 0 // supposed to be 1 4 7
values in column 2:
8 0 0 // supposed to be 2 5 8
I think that the problem lies in my understanding of pointers not the algorithm implemented. Actually, at first I didn't use pointer in my get_col function like below:
int result[ncol];
// ... do my work here to populate array
return &result;
Then as compiler complains warning: function returns address of local variable [-Wreturn-local-addr], I converted result from array to pointer in get_col function like above. What is the problem in this code? Did I use pointers in get_col function as it should be?
In the following line:
*result = *(arr + i*ncol + ind);
You're always writing to the same memory address.
Change it to one of the two following options:
*(result + i) = *(arr + i*ncol + ind);
result[i] = *(arr + i*ncol + ind);
Regarding your second problem when you used:
int result[ncol];
// ... do my work here to populate array
return &result;
You should understand that result variable in this case (static-memory allocation) is stored in the stack. So, after your function returns, the variable values doesn't exist anymore in the memory. That's why you need dynamic-memory allocation. In dynamic-memory allocation, that values stay in the memory until you call free by yourself.
I'm exercising a bit in programming C.
One task is to concat two dynamic arrays. The elements of the second array should be added to the end of the first array. The following is given:
void concatArrays(int* numbers1, int length1, int* numbers2, int length2)
{
//code
}
And that is my code to solve the task:
#include <stdio.h>
#include <stdlib.h>
void concatArrays(int* numbers1, int length1, int* numbers2, int length2)
{
numbers1 = (int*)realloc(numbers1, sizeof(int*) * (length1 + length2));
for (int count = 0; count < length2; count++)
{
numbers1[length1 + count] = numbers2[count];
}
}
int main()
{
int* num = (int*)malloc(sizeof(int*) * 6);
num[0] = 1;
num[1] = 2;
num[2] = 3;
num[3] = 4;
num[4] = 5;
num[5] = 6;
int* numbers = (int*)malloc(sizeof(int*) * 4);
numbers[0] = 1;
numbers[1] = 2;
numbers[2] = 3;
numbers[3] = 4;
concatArrays(num, 6, numbers, 4);
for (int count = 0; count < 10; count++)
{
printf("%d - ", num[count]);
}
return 0;
}
Unfortunately, it doesn't work. I do know that the code does work if I used a pointer to a pointer:
void concatArrays(int** numbers1, int length1, int** numbers2, int length2) { //code }
Nonetheless, that seems to be not allowed regarding the task requirements.
Do you have any idea how I could change my code meeting the requirements to solve the task?
Thank you in advance.
Edit:
I forgot:
The output:
1 - 2 - 3 - 4 - 5 - 6 - 2054454589 - 32767 - -1280384664 - 32767 -
void concatArrays(int* numbers1, int length1, int* numbers2, int length2)
Given prototype is pass by value for your case.
Hence when you reallocate the memory.
numbers1 = (int*)realloc(numbers1, sizeof(int*) * (length1 + length2));
You are allocating for local copy not for original copy.
Note that it is not guaranteed that new pointer returned by realloc
will be same as old.
code does work if I used a pointer to a pointer.
That is because you will be passing by reference any modification inside function will update the original variables.
Thus allocate the more memory in the main itself.
int* num = (int*)malloc(sizeof(int) * 10);
There is a failure in the memory allocation, respective on type.
Note that the values of array are ints, so by the allocation they occupy not a pointer size memory more likely int size spaces:
so the allocation should look like this for int arrays:
void concatArrays(int** numbers1, int* length1, const int* numbers2, const int length2)
{
*length1 = *length1 + length2;
*numbers1 = (int*)realloc(*numbers1, sizeof(int) * (*length1));
...
}
Note that the size of an int pointer (int*) may be different than the size of a pure int type.
I'm trying to test a program that creates an array with 0 elements in it, then adds elements to it (reallocating memory each time), and then printing out the elements. But, I keep getting errors when I try to run it.
int main(int argc, const char * argv[]) {
int num = 0;
int n = 10;
int **array = malloc(0);
for (int i = 0; i < n; ++i)
{
++num;
array = realloc(array, num * sizeof(int*));
array[num-1] = &i;
}
for (int j = 0; j < n; ++j)
{
printf("%d", &array[j]); // error 1
}
return 0;
}
I'm sorry I didn't include the errors with the original post. I think I fixed one of them. Here is the other:
Error 1: Format specifies type 'int' but the argument has type 'int *'
This answer is based on the assumption that you are printing a simple array, since you don't show what output you expect. You are using one more step of indirection than you need, and too many variables. Take note that indexing is different from length (often by 1).
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int i, n = 10;
int *array = NULL; // no need for double star, or fake allocation
for (i = 0; i < n; ++i)
{
array = realloc(array, (i + 1) * sizeof(int)); // remove the *, add 1 for num elements
array[i] = i;
}
for (i = 0; i < n; ++i)
{
printf("%d", array[i]); // remove the &
}
free(array); // don't forget this
return 0;
}
Program output:
0123456789
In practice, you should assign the result of realloc to another pointer variable, check it's ok, and then replace the original pointer var.
printf("%d", &array[j]); // error 1
The & is the address operator. So it is making something a pointer. Your array is already an array of pointers to integer.
By using the & you are trying to print the address of the integer pointer itself.
Try using * instead of &. * means that you want to print the value of the integer pointer.
printf("%d", *array[j]); // error 1
I would like to dynamically allocate (malloc) a multidimensional character array in C. The array would have the following format:
char *array[3][2] = {
{"one","two"},
{"three","four"},
{"five","six"}
};
Before the array would be created, I would already know the number of rows and the lengths of all of the characters arrays in the multidimensional array.
How would I malloc such a character array?
Thanks in advance!
This is one way to allocate a two dimensional array of char *.
Afterwards, you can assign the contents like a[1][2] = "foo";
Note that the elements of the array are initialized to (char *)0.
#include <stdio.h>
#include <stdlib.h>
char ***alloc_array(int x, int y) {
char ***a = calloc(x, sizeof(char **));
for(int i = 0; i != x; i++) {
a[i] = calloc(y, sizeof(char *));
}
return a;
}
int main() {
char ***a = alloc_array(3, 2);
a[2][1] = "foo";
printf("%s\n", a[2][1]);
}
[Charlies-MacBook-Pro:~] crb% cc xx.c
[Charlies-MacBook-Pro:~] crb% a.out
foo
First of all, arrays are typically stored in Row Major form, so in reality you have a vector six elements long, each entry is a char * ptr. That is, the elements labelled by row, column are similar to:
char *r1c1, *r1c2, *r2c1, *r2c2, *r3c1, *r3c1;
Thus, do a SIMPLE malloc of:
char *matrix = malloc(3*2*sizeof(char *));
Then set the elements as:
matrix[0] = "one";
matrix[1] = "two";
matrix[2] = "three";
matrix[3] = "four";
matrix[4] = "five";
matrix[5] = "six";
Finally, to test this write a nested loop as:
for (int r=0; r<3; r++)
{
for (int c=0; c<2; c++);
{
printf("%s\n",matrix[r][c]);
}
}
Note, how a matrix is treated first as a vector then as a matrix. C doesn't care!!
char *array[3][2] is nothing but a two dimensional array of pointers. Hence you need the storage space of 3*2*sizeof(char *) to store the pointers.
As you mentioned, the pointers are actually pointing to zero-terminated strings and you may like the strings to be malloc'ed as well. Assuming the total length of all the strings to be N (including zero-termination), the storage space needed is (3*2*sizeof(char *) + N).
Allocate memory for the above mentioned size and the copy the strings yourselves as below.
In the following code, we assume that the number of columns (2) is a constant
char *(*dst)[2] = (char *(*)[2]) malloc(3*2*sizeof(char *) + N);
char * s = ((char *) dst) + (3*2*sizeof(char *));
for (i = 0; i < 3; i++)
{
for (j = 0; j < 2; j++)
{
strcpy(s, src[i][j]);
dst[i][j] = s;
s += strlen(s)+1;
}
}
NOTE: In the above code, 'dst' is a pointer that points to the first row of the 2D array of char *.
If the number of columns is not constant, the syntax changes a bit, but the storage size is the same.
char **dst = (char **) malloc(3*2*sizeof(char *) + N);
char * s = ((char *) dst) + (3*2*sizeof(char *));
for (i = 0; i < 3; i++)
{
for (j = 0; j < 2; j++)
{
strcpy(s, src[i][j]);
dst[i*2 + j] = s; /* 2 is the number of columns */
s += strlen(s)+1;
}
}
NOTE: Here 'dst' is a pointer that points to the first element of 1D array of char * and the 2D indexing is done manually.
The above examples assume that the string lengths will not change after allocation. If the strings can change at any point in time after allocation, then it is better to allocate for each string separately.
Keep it simple, Sheldon. The answer you've selected uses a char ***, which is not even close to the equivalent of a char *[2][3]. The difference is in the number of allocations... An array only ever requires one.
For example, here's how I'd retro-fit the answer you selected. Notice how much simpler it is?
#include <stdio.h>
#include <stdlib.h>
void *alloc_array(size_t x, size_t y) {
char *(*a)[y] = calloc(x, sizeof *a);
return a;
}
int main() {
char *(*a)[2] = alloc_array(3, 2);
a[2][1] = "foo";
printf("%s\n", a[2][1]);
}
In case you arrive in this page, wanting to create an array like int myarray[n][M] (which is slighly different from the question since they want an array of string), where M is fixed and n can vary (for example if you want an array of coordinates...), then you can just do:
int (*p)[M] = malloc(n*sizeof *p);
and then use p[i][j] as before. Then, you will get sizeof p[i] = M*sizeof(int):
#include <stdio.h>
#include <stdlib.h>
#define M 6
int main(int argc, char *argv[])
{
int n = 4;
int (*p)[M] = malloc(n*sizeof *p);
printf("Size of int: %lu\n", sizeof(int));
printf("n = %d, M = %d\n", n, M);
printf("Size of p: %lu (=8 because pointer in 64bits = 8 bytes)\n", sizeof p);
printf("Size of *p: %lu (=M*sizeof(int) because each case is an array of length M)\n", sizeof *p);
printf("Size of p[0]: %lu (=M*sizeof(int) because each case is an array of length M)\n", sizeof p[0]);
// Assign
for (int i=0; i<n; i++) {
for (int j=0; j<M; j++) {
(p[i])[j] = i*10+j;
}
}
// Display
for (int i=0; i<n; i++) {
for (int j=0; j<M; j++) {
printf("%2d; ", (p[i])[j]);
}
printf("\n");
}
return 0;
}
which gives:
Size of int: 4
n = 4, M = 6
Size of p: 8 (=8 because pointer in 64bits = 8 bytes)
Size of *p: 24 (=M*sizeof(int) because each case is an array of length M)
Size of p[0]: 24 (=M*sizeof(int) because each case is an array of length M)
0; 1; 2; 3; 4; 5;
10; 11; 12; 13; 14; 15;
20; 21; 22; 23; 24; 25;
30; 31; 32; 33; 34; 35;