while allocating/printing why don't I have to dereference an array pointer in c?

Advertisements #include <stdio.h> void main(){ int *i = (int *) malloc(12); *i[0] = 1; printf("%d", *i[0]); } i is supposed to be a pointer. So then why doesnt this code work? and why does this code 👇 work? void main(){ int *i = (int *) malloc(12); i[0] = 1; printf("%d", i[0]); } >Solution : When… Read More while allocating/printing why don't I have to dereference an array pointer in c?

Possible to Initialize pointer to array of typed variables?

Advertisements I have been looking at c pointer to array of structs , and trying to apply it to my example. I arrived at this code snippet, which compiles fine: #include <stdio.h> enum test_e { eONE, eTWO, eTHREE, }; typedef enum test_e test_t; #define NUMITEMS 12 extern test_t my_arr[NUMITEMS]; test_t (*my_arr_ptr)[NUMITEMS]; test_t my_arr[NUMITEMS] = {… Read More Possible to Initialize pointer to array of typed variables?

What's the best way to initialize a dynamically allocated boolean 2d array?

Advertisements Just trying to (re)learn C and struggling with some basic concepts. I’ve took a look this thread: How to memset an array of bools?. According to that, it seems i may initialize a boolean 2d array like the following: size_t size = sizeof(bool[4][3]); bool (*ary)[4] = malloc(size); memset(ary, false, size); The problem i see… Read More What's the best way to initialize a dynamically allocated boolean 2d array?

Why are is the address of a pointer different when accessed in two different ways?

Advertisements Why does the following code output two different pointer addresses for one of the Timer._d objects in the vector? This is a minimal example of the issue I’m seeing — see below for a complete picture. #include <cstdio> #include <vector> #include <iostream> struct Data { int duration = 0; }; class Timer { private:… Read More Why are is the address of a pointer different when accessed in two different ways?

How to change the value of a variable passed as an argument and also use the value for an array

Advertisements This is my example code: #include <stdio.h> void Func(int a[], int b) { a[b] = 1; b += 5; } int main(void) { int a[10]; int b = 0; printf("%d\n", b); Func(a, b); printf("%d\n", a[0]); printf("%d\n", b); } And I want the program to print: 0 1 5 I’ve tried changing the function by… Read More How to change the value of a variable passed as an argument and also use the value for an array

My program adds to the counter variable when I use "count+=1" and doesn't when I use "count++"

Advertisements I’m practicing pointers right now. The task is simple, count the number of instances of the letter c in an array and print the amount of times it occurred.I assigned a pointer to a counter variable in a function outside of main. For some reason, my program doesn’t count when I use *count++; #include… Read More My program adds to the counter variable when I use "count+=1" and doesn't when I use "count++"