在C++项目中,我使用的是包含一些C11标头的C库.它不会与GCC编译.看到这个简单的代码:
// main.cc
#include <stdatomic.h>
int main()
{
return 0;
}
Run Code Online (Sandbox Code Playgroud)
跑步gcc main.cc -lstdc++,抱怨:error: ‘_Atomic’ does not name a type.然而,clang main.cc -lstdc++工作就像一个魅力.我想知道是什么产生了影响,我怎么能用gcc编译它?
我曾尝试实现字符串 n 复制功能。我已经成功了,但我试图优化我的代码,但它不起作用。
char *mystrncpy(char *dst, const char *src, size_t n)
{
int i;
char *temp;
temp = dst;
for (i = 0; i < n; i++)
*dst++ = *src++;
return temp;
}
Run Code Online (Sandbox Code Playgroud)
上面的代码工作正常。
但是下面的有一些问题。它只是src为n我传递的任何值返回字符串。
char *my_strncpy(char *dst, const char*src, size_t n)
{
char *temp = dst;
while ((*dst++ = *src++) && (--n));
return temp;
}
Run Code Online (Sandbox Code Playgroud) 我对Head First C的以下程序有疑问.在主要功能下的书中,作者没有使用过search_for[strlen(search_for) - 1] = '\0';; 他的程序运行正常.但是,当我使用该程序的原始版本(按照书)时,它无法找到我输入的文本.我从github获得了以下版本(可以在字符串中找到文本),但我仍然无法理解为什么使用它.如果有人能解释我,我会非常感激.
#include <stdio.h>
#include <string.h>
char tracks[][80] = {
"I left my heart in Harvad Med School",
"Newark, Newark - Wonderful town",
"Dancing with a Dork",
"From here to maternity",
"The girl from Iwo Jima",
};
void find_track(char search_for[])
{
int i;
for (i = 0; i < 5; i++) {
if (strstr(tracks[i], search_for))
printf("Track %i: '%s'\n", i, tracks[i]);
}
}
int main()
{
char search_for[80];
printf("Search for :");
fgets(search_for, 80, …Run Code Online (Sandbox Code Playgroud) 列表的每个节点与数据一起保存后继节点的地址。这就是为什么递归可以很好地工作的原因,例如下面的示例:它接受输入,例如123456,然后将其打印为列表6 -> 5 -> 4 -> 3 -> 2 -> 1 -> NULL.
/* Structure types ----------------------------------------------*/
typedef int data;
struct list_element {
int data;
struct list_element *next;
};
typedef struct list_element ELEM;
typedef ELEM *LINK;
/* Recursive list create and list print---------------*/
LINK create_list(int n) {
if (n == 0) {
return NULL;
}
else {
LINK head = (LINK) malloc(sizeof(ELEM));
head -> data = n % 10;
head -> next = create_list(n / …Run Code Online (Sandbox Code Playgroud) 在独立函数中使用 while 循环打印字符串时遇到问题。
我有以下代码:
#include <stdio.h>
int pword(char *);
int main() {
char s[] = "Alice";
pword(s);
return 0;
}
int pword(char *s) {
while(*s!='\0') {
printf("%s", s);
s++;
}
printf("\n");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这是打印:Aliceliceicecee。
我正在写需要限制使用输入字符只能从代码A到H.那么大的H不应该被接受.我看到数字,我可以使用如下:
if (input == 0 - 9) return 1;
Run Code Online (Sandbox Code Playgroud)
但是,我怎么做,A到H(char)?
我试图解决这个特殊问题:项目欧拉#16:
2 ^ 15 = 32768,其数字之和为3 + 2 + 7 + 6 + 8 = 26.
数字2 ^ 1000的数字之和是多少?
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int num;
cin >> num;
long double n = pow(2, num);
cout << fixed << n << endl;
int sum = 0;
while(n != 0.0)
{
sum = sum + fmod(n, 10);
n = floor(n/10);
}
cout << sum << endl;
}
Run Code Online (Sandbox Code Playgroud)
用户输入为1000时的输出为:
Run Code Online (Sandbox Code Playgroud)1181
然而,互联网提到了正确的答案,1366而不是1181我得到的.
char txt[20] = "Hello World!\0";
Run Code Online (Sandbox Code Playgroud)
上面的定义分配了多少字节?考虑一个char占用1个字节,一个int2字节.请注意,只有一个",\0最后.如何计算上面定义占用的很多字节?
将输入作为十六进制字符串,然后将其转换为C中的char字符串.十六进制字符串可以包含0x00,转换后转换为Ascii中的0.这会终止字符串.我必须将值存储在char字符串中,因为API使用它.
我的代码到目前为止:
int hex_to_int(unsigned char c) {
int first =0;
int second =0;
int result=0;
if(c>=97 && c<=102)
c-=32;
first=c / 16 - 3;
second =c % 16;
result = first*10 + second;
if(result > 9) result--;
return result;
}
unsigned char hex_to_ascii(unsigned char c, unsigned char d){
unsigned char a='0';
int high = hex_to_int(c) * 16;
int low = hex_to_int(d);
a= high+low;
return a;
}
unsigned char* HextoString(unsigned char *st){
int length = strlen((const char*)st);
unsigned char* result=(unsigned char*)malloc(length/2+1); …Run Code Online (Sandbox Code Playgroud) 不知道我的问题在哪里.我尝试编译一次代码并且它有效.但是,我将它复制到一个新项目中以便修改它,只是颜色或等待的时间,它只适用于第一个程序.怎么了?我感到非常沮丧.使用此代码,我想绘制正弦函数.这是我的代码:
#include <bits/stdc++.h>
using namespace std;
int main()
{
int t,n1,n2,res=0;
cin>>t;
vector<int> v1;
vector<int> v2;
while (t--) {
cin>>n1>>n2;
v1.push_back(n1);
v2.push_back(n2);
}
for (int i=v1.size();i>0;i++) {
bool state=binary_search(v2.begin(),v2.end(),v1[i-1]);
if (state){
v1.pop_back();
res++;
}
}
cout<<res<<endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)