我正在尝试创建一个将返回int的C函数,但在此过程中将填充作为变量传入的char*.我正在尝试的一个基本示例是:
int myMethod(int input, char* output, int outMaxLen) {
int outValue = input * 5;
if (out < 10) strcat(output, "A small number");
else if (out < 20) strcat(output, "A medium number");
else strcat(output, "A large number");
}
Run Code Online (Sandbox Code Playgroud)
在main.c中:
char* myMethodOutput;
int myMethodInt = myMethod(2, myMethodOutput, 15);
printf("%d %s", myMethodInt, myMethodOutput);
Run Code Online (Sandbox Code Playgroud)
运行时,整数显示在屏幕上,但文本不显示.
outMaxLen变量用于检查char*参数以确保它足够大以容纳输出字符串.
和strcat()一样,我尝试过strcpy()和strncpy(),但都无济于事.strcat()不向控制台显示任何文本,strcpy()和strncpy()使用消息EXC_BAD_ACCESS调用调试器.
我已经使用strcpy_s函数在Windows API中成功地管理了这个,但我现在正在尝试UNIX框.我可能错过了一些非常基本的东西!
void slice_first_char(char ** string)
{
*string = &(*string[1]);
}
int main(int argc, char * argv[])
{
char * input = "abc";
input = &(input[1]);
puts(input); // "bc" as expected.
slice_first_char(&input);
puts(input); // \372\277_\377
// What‘s going on?
}
Run Code Online (Sandbox Code Playgroud)
如何重写slice_first_char函数以使其按预期工作?
我们的教授在网上发布了一个自定义的"String"模板文件,并在不久前问我们填写下面的功能.我的问题是,为了尝试理解这一点,这就是为什么排名前三的构造函数Text = NULL;在其下面,以及它的this = source;其他形式.我觉得每个人都应该说Text = the_input_parameter.
非常感谢,这是代码:
class String
{
public:
// Default constructor
String()
{
Text = NULL;
}
String(const String& source)
{
Text = NULL;
// Call the assignment operator to perform deep copy
*this = source;
}
String(const char* text)
{
Text = NULL;
// Call the assignment operator to perform deep copy
*this = text;
}
~String()
{
delete[] Text;
}
// Assignment operator to perform deep copy
String& …Run Code Online (Sandbox Code Playgroud) 帮助我摆脱这个问题.我在ubuntu12.04上使用GCC.当我编写这个程序从键盘n获取5个字符串然后在屏幕上打印这些字符串.程序已编译但在执行期间它从键盘获取字符串但仅打印最后一个字符串.我写的程序如下:
void main()
{
char names[10];
int i,j;
for(i=0;i<5;i++)
{
printf(" Enter a name which you want to register\n");
scanf("%s",names);
}
for(i=0;i<5;i++)
printf(" the names you enter are %s\n", names);
}
Run Code Online (Sandbox Code Playgroud) 所以我正在学习使用编译器Dev C++编程c.问题1:
#include <stdio.h>
#include <conio.h> //for the getch() function
#include <string.h>
int main(void)
{
char line[3];
strcpy(line, "Hello world");
printf("%s", line);
getch();
}
Run Code Online (Sandbox Code Playgroud)
输出:Hello world
为什么当我声明我的字符串只能容纳3个字符时,它会显示所有"Hello world"?
问题2:
char line[3] = "Hello world";
printf("%s", line);
Run Code Online (Sandbox Code Playgroud)
输出:Hel
为什么显示"Hel"?它不应该只显示"He",因为行[0] = H,行[1] = e和行[2] ='\ 0'?%s通过搜索'\ 0'来工作?
请帮我理解真正发生的事情.谢谢!
我是C的新手,所以我不太熟悉它的语法,但是我调试了我的代码并研究了正确的语法,它似乎是正确的,我也改变了变量的范围,看看这是不是导致错误.
if语句应该比较两个变量,它们都保存字符串,我甚至打印出两个变量以确保它们是相同的,但是它仍然直接跳到if语句的else部分.任何人都可以给我任何关于为什么它不会运行if语句的指针,它只是直接跳到'不正确'.
该correctWord变量在代码中的一个不同的部分中定义.
在此查找完整代码.
-UPDATE-
我现在已经更新了代码的语法,但它仍然返回false.

char correctWord[20];
void userGuess(){
char userWordGuess[20];
printf("Anagram: ");
printf(anagramWord);
printf("Your Guess: ");
scanf("%s",userWordGuess); //Reads in user input
printf(correctWord);
printf(userWordGuess);
if(strcmp(userWordGuess, correctWord) == 0){
printf("Congratulations, you guessed correctly!");
}else{
printf("Incorrect, try again or skip this question");
}
}
Run Code Online (Sandbox Code Playgroud) 如何使用c-string并避免溢出?例如,如果我有代码:
#include <iostream>
using namespace std;
int main()
{
int size = 1000;
char * name = new char[size];
getline(cin, name);
}
Run Code Online (Sandbox Code Playgroud)
我不知道这个人的名字会有多长,所以你怎么避免溢出?如果我作为预防措施分配1000,他们可以轻松输入1001个字符.我该怎么做才能阻止这种情况发生?
编辑:我必须能够在没有字符串类的情况下执行此操作
所以我参加了我大学的初级水平系统课程,我的教授提到数组大小无法更改,因为它们是在内存中设置的.
此外,C中的字符串只是字符数组.
所以我的问题是,当长度本身无法改变时,如何使用strcpy复制不同长度的字符串?
(目前凌晨2:30,1天内参加考试)
抱歉这是一个初学者的问题,但非常感谢任何帮助!! 谢谢 :)
char* str ="Hello";
Run Code Online (Sandbox Code Playgroud)
在上面的代码中,文字“ Hello”存储在DATA段中,并且是只读的。所以总是声明它不是更好:
const char* str = "Hello";
Run Code Online (Sandbox Code Playgroud)
避免使用不正确的代码,例如:
*(str+1) = 't';
Run Code Online (Sandbox Code Playgroud)