#assign char array by loop, then out output in order by loop

54 messages · Page 1 of 1 (latest)

ember vine
#

            int i;
            int i2;


            printf("enter char array amount\n");
            scanf("%d",&i);

            char char1[i];

            char1[0] = 'A';

            for(i2=0;i2<i;i2++){
                char1[i] = char1[i-1]++;
            }


            for(i2=0;i2<i;i2++){
                printf("%c\n",char1[i2]);
            }

idle sundialBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

ember vine
#

a bit confused so far

pulsar trench
#

considering char1[i] = char1[i-1]++; is UB - so am i

#

what are you trying to do exactly?

ember vine
#

so, to input number,

#

then output from A to x depending on input

#

so if 7 is entered
output:
A
B
C
D
E
F
G

pulsar trench
#

do you have to use an array for that? why don't you just print it one char at a time?

ember vine
#

i could, but the array version is more compact, its part of the excercise

pulsar trench
#

is more compact
its most certainly not, but given its an exercise - it doesn't really matter

#

regardless (assuming you're fine with VLAs) - consider the value of i in this loop

for(i2=0;i2<i;i2++){
  char1[i] = char1[i-1]++;  // whats the value of i here?
}```
ember vine
#

0

#

shold be 1 i think

#

because [0] is already A

pulsar trench
#

no. its whatever your user supplied you with

pulsar trench
#
int i;
printf("enter char array amount\n");
scanf("%d", &i);

char char1[i];  // VLA

...

for(int j = 0; j < i; j++){
    char1[i] = char1[i-1]++;
}```
does this makes it clearer?
#

j and i are 2 different variables. i was never changed (i.e. its equal to the last index of the array + 1) yet the loop uses i to access the array for some reason

ember vine
#

i get the loop

#

its just the

pulsar trench
#

no. its not

#

at this point i suggest you run through that snippet with either a pen & paper / through a debugger and see what is the value of i throught the iteration

ember vine
#

char1[1] = char1[1]+ char1[-1];

#

which is sopposed to set to 'B', but it makes it a 'Y' instead

pulsar trench
#

thats still UB. you're not allowed to access char[-1]

ember vine
#

its assign array[1], not [0]

#

[0] is automatically a A from the coding

pulsar trench
#

and after you've done that - i suggest you first figure out how to print the chars rather than storing them in the array first

ember vine
#

ok

#

char1[0] = 'A';
done

pulsar trench
ember vine
#

char1[0]++;

changed from A to B

#

so how would you make array[0] (which is A) an array[1] to a B

pulsar trench
#

i would use the correct index in a loop

ember vine
#

ye you need the correct loop, also you need the assignment within the loop

pulsar trench
#

sure. compare this loop to yours

for(size_t i = 0; i < size; i ++) {
  arr[i] = 'A' + ...;
}```
ember vine
#

so, this works

char1[1] = char1[0]++;

#

assigns B from A

pulsar trench
#

sure. i suggest you won't do it that way. refer to my loop above

ember vine
#

k

#

whats the . . .

pulsar trench
#

its a value you need to figure out

ember vine
#

A + 1?

#

A + loop int?

pulsar trench
#

are you just guessing?

ember vine
#

no

#

i guess

the loop

A + x(1) = B
A + x(2) = C

could work

ember vine
#

ive figured it out

#

how do i show the code on here?

#
            int i2;


            printf("enter char array amount\n");
            scanf("%d",&i2);

            char char1[i2];

            char1[0] = 'A';


            for(i=1;i<i2;i++){
                char1[i] = (char1[i-1])+1;
            }

            for(i=0;i<i2;i++){
                printf("%c\n",char1[i]);
            }```
idle sundialBOT
#

@ember vine Has your question been resolved? If so, type !solved :)

ember vine
#

!solved