Dynamic Array Size Problem??

Joined
Jul 10, 2023
Messages
5
Reaction score
0
Hello I am new to coding and was just wandering why the below code:

#include <stdio.h>

int main() {

for(int i = 0; 1 ; i++) {

char x;
char z[1+i];
x=getchar();
if (x == '\n'){
*(z+i) = '\0';
printf("%s",z);
break;}
*(z+i) = x;
printf("%s, %d = %c, i = %d\n",z, (z+i),*(z+i),i);
}

return 0;
}

of C does not work for inputs that are more than 15 characters? (I don't think it is beacuse of the current state of my pc since I tried it with an online compiler and it still breaks at 16 chars, but then again I am new to coding.) Would appreciate any help. Thank you.
 
Joined
Jul 10, 2023
Messages
5
Reaction score
0
Well it may not be, but I thought the question itself was beginner level for someone with a good C knowledge.
 
Joined
Jul 10, 2023
Messages
5
Reaction score
0
Well I want it to not break for inputs that are longer than 15 characters. Thats all. Nothing more, nothing less.
 
Joined
Nov 24, 2022
Messages
80
Reaction score
7
Please change z+i to int in printf() function, as the compiler wrote.
 

Attachments

  • 111111a3.jpg
    111111a3.jpg
    74.6 KB · Views: 15
Joined
Jul 10, 2023
Messages
5
Reaction score
0
Code:
#include <stdio.h>

int main() {

for(int i = 0; 1 ; i++) {

    char x;
    char z[1+i];
    x=getchar();
    if (x == '\n'){
        *(z+i) = '\0';
        printf("%s",z);
        sizeof(z);
        break;}
    *(z+i) = x;
    printf("%s, %p = %c, i = %d\n", z, (z+i), *(z+i), i);
}



return 0;
}

I changed the variable type to pointer instead of int. Now adresses show up in hexadecimal (I guess??)

Code:
1234567890123456
1, 000000000061FDC0 = 1, i = 0
12, 000000000061FDC1 = 2, i = 1
123, 000000000061FDC2 = 3, i = 2
1234, 000000000061FDC3 = 4, i = 3
12345, 000000000061FDC4 = 5, i = 4
123456, 000000000061FDC5 = 6, i = 5
1234567, 000000000061FDC6 = 7, i = 6
12345678, 000000000061FDC7 = 8, i = 7
123456789, 000000000061FDC8 = 9, i = 8
1234567890, 000000000061FDC9 = 0, i = 9
12345678901, 000000000061FDCA = 1, i = 10
123456789012, 000000000061FDCB = 2, i = 11
1234567890123, 000000000061FDCC = 3, i = 12
12345678901234, 000000000061FDCD = 4, i = 13
123456789012345, 000000000061FDCE = 5, i = 14
1234567890123456p@, 000000000061FDCF = 6, i = 15

Process returned 0 (0x0)   execution time : 4.757 s
Press any key to continue.

The issue persists.

PS maybe it has something to do with hexadecimals since it is base16.
 

Attachments

  • image.png
    image.png
    219.9 KB · Views: 10
Last edited:
Joined
Nov 24, 2022
Messages
80
Reaction score
7
I let myself to consult AI.
It wrote

"When you run this code, it will continuously read characters from the standard input until you enter a newline character (Enter key). As you type each character, it will be printed along with its index, memory address, and ASCII value. Once you enter the newline character, it will print the entire input string and terminate."

This is the answer I expected from You.

"The line char z[1 + i]; is not valid in standard C syntax. In C, the size of an array must be a constant expression known at compile-time."

This shouldn't be allowed.

Size of an array in C is constant not without a reason.
Now You know , that it is possible.
In programming everything is possible.
 
Joined
Jul 10, 2023
Messages
5
Reaction score
0
Well, I consulted ChatGPT many times and it gave me a different answer every time about the source of the error. I also asked about if dynamic arrays were possible in C, it told me something similar to what you said "the size of an array must be a constant expression known at compile-time". But when I asked about detailed info it said it is possible to have dynamic array sizes in C and apoligized. Well to test this, I modified the program to include the size of the array for each iteration:
Code:
#include <stdio.h>

int main() {

for(int i = 0; 1 ; i++) {

    int size;
    char x;
    char z[1+i];
    x=getchar();
    if (x == '\n'){
        *(z+i) = '\0';
        printf("%s",z);
        break;}
    size = sizeof(z);
    *(z+i) = x;
    printf("%s, %p = %c, i = %d, %d \n", z, (z+i), *(z+i), i, size);
}

return 0;
}

where the output is now:

Code:
1234567890123456
1, 000000000061FDC0 = 1, i = 0, 1
12, 000000000061FDC1 = 2, i = 1, 2
123, 000000000061FDC2 = 3, i = 2, 3
1234, 000000000061FDC3 = 4, i = 3, 4
12345, 000000000061FDC4 = 5, i = 4, 5
123456, 000000000061FDC5 = 6, i = 5, 6
1234567, 000000000061FDC6 = 7, i = 6, 7
12345678, 000000000061FDC7 = 8, i = 7, 8
123456789, 000000000061FDC8 = 9, i = 8, 9
1234567890, 000000000061FDC9 = 0, i = 9, 10
12345678901, 000000000061FDCA = 1, i = 10, 11
123456789012, 000000000061FDCB = 2, i = 11, 12
1234567890123, 000000000061FDCC = 3, i = 12, 13
12345678901234, 000000000061FDCD = 4, i = 13, 14
123456789012345, 000000000061FDCE = 5, i = 14, 15
1234567890123456, 000000000061FDCF = 6, i = 15, 16

Process returned 0 (0x0)   execution time : 4.046 s
Press any key to continue.

It seems to me that arrays can be initilized with variables, but the question still remains, why does this method vreak after 15 chars of input?
 

Attachments

  • image2.png
    image2.png
    23.2 KB · Views: 7
Joined
Nov 24, 2022
Messages
80
Reaction score
7
I call it "control" xD masterpiece xD In polish internet such posts like your disapper very quickly.
It seems, they don't understand our sense of humour.
 
Last edited:

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,061
Latest member
KetonaraKeto

Latest Threads

Top