Home > Enterprise >  is there a way to fix my arrays in c not showing the stored number in terminal
is there a way to fix my arrays in c not showing the stored number in terminal

Time:05-27

enter image description herethe image is of my code on vscode running on a chromebook duet linux mint.I have a slight feeling i did not create my launch.json file corrrectly but im not sure help me out

CodePudding user response:

The ampersand & in &nums[i] is redundant. You are printing the address of nums[i], instead of the value of nums[i].

A fixed version would be:

#include <stdio.h>

int main() {
    const int nums[] = { 10, 12, 13, 13 };
    for (int i = 0; i < 4;   i) {
        printf("%d\n", nums[i]);
    }
    return 0;
}

CodePudding user response:

It obviously needs more & and pointers.

#include <stdio.h>

int main(void) {
    int nums[] = {10, 12, 13, 13};
    int *p;
    for(p = nums; p < &nums[3];   p) {
        printf("%d\n", *p);
    }
    return 0;
}
  • Related