my code is as follows:
我的代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void confab(const char* inText, int nRows, char* outText)
{
int inText_length = 0;
printf("inText 1 = %c\n\n", *inText);
for (; *inText != '\0'; inText_length++){
printf("%d %c\n", inText_length, *inText);
inText++;
}
for (int z = inText_length; inText_length > 1; z--){
inText--;
}
printf("inText 2 = %c\n\n", *inText);
printf("This line doesn't print");
}
int main(void)
{
char buffer[60] = {'\0'};
char* s = "Don't wait until the last day before starting.";
confab(s, 3, buffer);
//printf("%s\n", buffer);
return EXIT_SUCCESS;
}
Basically, what I'm trying to do is keep inText a constant and get the length of the input string used in the main function. (I'll then do more things using that length later) So what I've done is start two for loops to get the length of inText by incrementing it, then decrementing it so it's back in its original state.
基本上,我要做的是保持inText一个常量,并得到在主函数中使用的输入字符串的长度。(稍后我将使用这个长度做更多的事情),所以我所做的是通过递增它的循环来获得inText的长度,然后递减它,这样它就回到了原来的状态。
But for whatever reason the last two printf functions aren't showing up. Also, I can't use the line
但不管什么原因,最后两个printf函数没有出现。还有,我不能用这条线。
inText - inText_length;
for whatever reason, it says the line doesn't do anything, so I've tried to replace it with the second for loop.
不管出于什么原因,它说这条线什么也没做,所以我试着用第二个for循环替换它。
Thanks!
谢谢!
2 个解决方案
#1
2
When I look at this line:
当我看这条线的时候:
for (int z = inText_length; inText_length > 1; z--){
inText--;
}
I think what you mean is this:
我认为你的意思是
for (int z = inText_length; z > 1; z--){
inText--;
}
Instead of using
而不是使用
inText - inText_length;
you should use
你应该使用
inText = inText - inText_length;
or
或
inText -= inText_length
#2
1
First of all, why are you not using strlen ? You have an infinite loop in you function, indeed:
首先,你为什么不使用strlen ?你在函数中有一个无限循环,实际上
for (int z = inText_length; inText_length > 1; z--){
inText--;
}
You're decrementing z but using inText_length
in the condition, here is the fix:
您正在减少z,但是在条件中使用inText_length,这里是修复:
for (int z = inText_length; z > 1; z--){
inText--;
}
Is you don't want to use strlen, you should at least use another pointer to avoid the second loop:
您是否不想使用strlen,至少应该使用另一个指针来避免第二个循环:
char* ptr = inText;
for (; *ptr!= '\0'; inText_length++){
printf("%d %c\n", inText_length, *ptr);
ptr++;
}
#1
2
When I look at this line:
当我看这条线的时候:
for (int z = inText_length; inText_length > 1; z--){
inText--;
}
I think what you mean is this:
我认为你的意思是
for (int z = inText_length; z > 1; z--){
inText--;
}
Instead of using
而不是使用
inText - inText_length;
you should use
你应该使用
inText = inText - inText_length;
or
或
inText -= inText_length
#2
1
First of all, why are you not using strlen ? You have an infinite loop in you function, indeed:
首先,你为什么不使用strlen ?你在函数中有一个无限循环,实际上
for (int z = inText_length; inText_length > 1; z--){
inText--;
}
You're decrementing z but using inText_length
in the condition, here is the fix:
您正在减少z,但是在条件中使用inText_length,这里是修复:
for (int z = inText_length; z > 1; z--){
inText--;
}
Is you don't want to use strlen, you should at least use another pointer to avoid the second loop:
您是否不想使用strlen,至少应该使用另一个指针来避免第二个循环:
char* ptr = inText;
for (; *ptr!= '\0'; inText_length++){
printf("%d %c\n", inText_length, *ptr);
ptr++;
}