Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用C语言构造字符串_C_String - Fatal编程技术网

用C语言构造字符串

用C语言构造字符串,c,string,C,String,这是我用来从字符数组构造字符串的演示代码,有没有更好的方法来构造字符串*RV200*FV200 int main() { char String4H1[10] = "*FV"; char String4H3[10] = "*RV"; char String4H2[10] = "#"; char data1[10]; char data2[10]; snprintf(data1,4, "%03d", 200); //Convert integer to string function

这是我用来从字符数组构造字符串的演示代码,有没有更好的方法来构造字符串
*RV200
*FV200

int main()
{    
char String4H1[10] = "*FV";
char String4H3[10] = "*RV";
char String4H2[10] = "#";

char data1[10];
char data2[10];

snprintf(data1,4, "%03d", 200); //Convert integer to string function
snprintf(data2,4, "%03d", 200); //Convert integer to string function

ConvertToString(String4H1,data1, 3);     //*FV200
ConvertToString(String4H3,data2, 3);     //*RV200

ConvertToString(String4H1,String4H2,6);   //*FV200#
ConvertToString(String4H3,String4H2,6);   //*RV200#

//Display String4H1 And String 4H3 

}


void ConvertToString(char subject[], const char insert[], int pos) 
{
char buf[100] = {};  
strncpy(buf, subject, pos);               // copy at most first pos characters
int len = strlen(buf);
strcpy(buf+len, insert);                 // copy all of insert[] at the end
len += strlen(insert);                  // increase the length by length of insert[]
strcpy(buf+len, subject+pos);          // copy the rest

strcpy(subject, buf);                 // copy it back to subject
                                 // deallocate buf[] here, if used malloc()
}
在程序开始时,数字
200
是未知的,它是使用IDE函数从内存中提取的,以从特定内存地址获取值。 例如:

unsigned short BUF = GetWord(@FrontVIB@,0);    
unsigned short BUF1 = GetWord(@RearVIB@,0);

//BUF and BUF1 stores the value of address @FrontVIB@ and @RearVIB@ respectively

**structure** :-
unsigned short GetWord( @Address Alias@, Address Offset );

这是一个简单的例子。我可能会被否决:)

输出

*RV200#

这是一个简单的例子。我可能会被否决:)

输出

*RV200#

有很多问题,我只是在解决
ConvertToString()


虽然有人试图解决字符串缓冲区问题,但OP的代码中存在太多漏洞。请考虑以下内容:

void ConvertToString(char subject[], const char insert[], int pos) {
  char buf[100] = {};  
  strncpy(buf, subject, pos);               // copy at most first pos characters
  int len = strlen(buf);
  ...
pos==100的影响是什么?
strlen(buf)
可以尝试查找没有空字符-->UB的字符数组的长度

pos>100的影响是什么?
strncpy()
尝试在
buf
的绑定之外写入数据

学究:pos<0
的影响是什么?
strncpy()
尝试在
buf
的绑定之外写入数据,因为
pos
被转换为过大的无符号数


关于
strcpy(buf+len,subject+pos)

如果
pos
超过
strlen(主题)

UB as code尝试在
主题
之外读取


重新写入尝试。关键元素:包括传入的扩展
主题的可用大小,并确定字符串长度。然后测试可接受性。在所有这些之后,移动
主题的末尾
插入
腾出空间

void ConvertToString(char subject[], size_t subject_size, const char *insert,
    size_t pos) {

  size_t insert_length = strlen(insert);
  size_t subject_length = strlen(subject);
  // Insure pos does not exceed subject_length, 
  // this critical part missing in OP code 
  if (pos > subject_length) {
    pos = subject_length;
  }

  // Insure we have enough space
  size_t buf_size = subject_length + insert_length + 1;
  if (buf_size > subject_size) {
    Handle_Insufficent_subject_size();
    return;
  }
  // Move end portion of `subject` over to the right `insert_length` places.
  memmove(subject + pos + insert_length, subject + pos, subject_length - pos + 1);
  memcpy(subject + pos, insert, insert_length); // copy insert
}

有很多问题,我只是在解决
ConvertToString()


虽然有人试图解决字符串缓冲区问题,但OP的代码中存在太多漏洞。请考虑以下内容:

void ConvertToString(char subject[], const char insert[], int pos) {
  char buf[100] = {};  
  strncpy(buf, subject, pos);               // copy at most first pos characters
  int len = strlen(buf);
  ...
pos==100的影响是什么?
strlen(buf)
可以尝试查找没有空字符-->UB的字符数组的长度

pos>100的影响是什么?
strncpy()
尝试在
buf
的绑定之外写入数据

学究:pos<0
的影响是什么?
strncpy()
尝试在
buf
的绑定之外写入数据,因为
pos
被转换为过大的无符号数


关于
strcpy(buf+len,subject+pos)

如果
pos
超过
strlen(主题)

UB as code尝试在
主题
之外读取


重新写入尝试。关键元素:包括传入的扩展
主题的可用大小,并确定字符串长度。然后测试可接受性。在所有这些之后,移动
主题的末尾
插入
腾出空间

void ConvertToString(char subject[], size_t subject_size, const char *insert,
    size_t pos) {

  size_t insert_length = strlen(insert);
  size_t subject_length = strlen(subject);
  // Insure pos does not exceed subject_length, 
  // this critical part missing in OP code 
  if (pos > subject_length) {
    pos = subject_length;
  }

  // Insure we have enough space
  size_t buf_size = subject_length + insert_length + 1;
  if (buf_size > subject_size) {
    Handle_Insufficent_subject_size();
    return;
  }
  // Move end portion of `subject` over to the right `insert_length` places.
  memmove(subject + pos + insert_length, subject + pos, subject_length - pos + 1);
  memcpy(subject + pos, insert, insert_length); // copy insert
}

为什么不对整个字符串使用
snprintf
?类似于snprintf(data1,sizeof(data1),%s%d%s),String4H1,200,String4H2)?@JoachimPileborg
“%s%d%s”
应该是
“%s%03d%s”
snprintf(data1,sizeof(data1),“%s%03d#”,String4H1,200)
char String4H1[]=“*FV”与其他代码相同
char buf[100]={}
你确定这应该是C?为什么不对整个字符串使用
snprintf
?类似于snprintf(data1,sizeof(data1),%s%d%s),String4H1,200,String4H2)?@JoachimPileborg
“%s%d%s”
应该是
“%s%03d%s”
snprintf(data1,sizeof(data1),“%s%03d#”,String4H1,200)
char String4H1[]=“*FV”与其他代码相同
char buf[100]={}
你确定这应该是C?考虑到你需要修改的数量,也许这个“简单”的断言也需要修改?@EOF我今天有点疯狂,我通过智能手机应用程序编写了整个代码:D我害怕丢失密码。:)因此,编辑比丢失更好。@EOF simple是指代码使用scanf和take fixed 3部分:字符串数字字符串。当
realloc()
失败时,您的
concatenateString()
出现内存泄漏,我不认为这比问题注释中讨论的
snprintf()
更可取。不是我的DV,有点复杂!这似乎不简单。考虑到你需要修改的数量,也许这个“简单”的断言也需要修改?@EOF我今天有点疯狂,我通过智能手机应用程序编写了整个代码D我害怕丢失密码。:)因此,编辑比丢失更好。@EOF simple是指代码使用scanf和take fixed 3部分:字符串数字字符串。当
realloc()
失败时,您的
concatenateString()
出现内存泄漏,我不认为这比问题注释中讨论的
snprintf()
更可取。不是我的DV,有点复杂!看起来并不简单。