℃,循环不工作 [英] C for loop not working
问题描述
#include <stdio.h>
char str[5][10]={"asdasdasd", "", "", "qweqweqwe", "asdasdasd"};
int main (){
change (4, 5, 3, 2);
}
void change (int r1, int c1, int r2, int c2){
int i=0;
for (i=0; i<2; i++){
str[r2][c2+i] = str[r1][c1+i];
}
for (i=0; i<2; i++){
str[r1][c1+i] = ' ';
}
return;
}
str是一个2-D弦。
当我调试它,第二个循环只是工作,并删除这些字符,但第一个行不工作。为什么?
str is a 2-d string. when I debug it, the second for loop just Works and deletes those characters, but the first row doesnt work. why?
误差,当我打印字符串,在[R1]的字符[C1]和[R1] [C1 + 1]被删除,但[R2] [C2]和[R2] [C2 + 1]是还是空的。
the error is, when I print the string, the chars at [r1][c1] and [r1][c1+1] is deleted but [r2][c2] and [r2][c2+1] is still empty.
推荐答案
我试图运行和我的R1和R2的输出
I tried to run that and my outputs for r1 and r2 are
qwdaweqwe
qwdaweqwe
asdas - SD(两个空格)
asdas--sd (two spaces)
和基本上它是什么code应该做的。
and basically it is what the code should be doing.
但是,如果我得到你的意图吧,你想那些空字符串工作。那么你就应该喂不同数量的功能,因为数组索引从0作品到n-1,这意味着如果你想使用第二个空字符串,使用指数2。我试过了,但有问题的声明。你可以试试这个
But if I get your intention right, you want to work with those empty strings. Then you should be feeding your function with different numbers, because array indexing works from 0 to n-1, meaning that if you want to use the second empty string, use index 2. I tried that, but there is problem with the declaration. You can try this
char str[5][10] = { "asdasdasd", " ", " ", "qweqweqwe", "asdasdasd" };
或一些细微的malloc为这两个空字符串。 code以上的作品。
or some fine malloc for those two empty strings. Code above works.
这篇关于℃,循环不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!