该程序将反转数组的内容。例如,如果数组包含{1,2,3,4,5},则它应显示{5,4,3,2,1}
for(i=0;i<n;i++)
{
tmp=a[i];
a[i]=a[n-1-i];
a[n-1-i]=tmp;
}
Run Code Online (Sandbox Code Playgroud) 我对c中的strcmp函数有疑问。作为家庭作业,我们会对字符串进行一些比较。但是尚不清楚,我们必须将数字与该函数进行比较。我知道还有其他比较方法可以比较数字,但是我们的教授是这样使用的。
当我使用这样的功能时:
char string1[] = "1";
char string2[] = "2"
printf("Compare between str1 and str2: %d, %d", strcmp(string1, string2));
Run Code Online (Sandbox Code Playgroud)
->输出:
Compare between str1 and str2: 1
Run Code Online (Sandbox Code Playgroud)
这意味着string1更大。
然后我只有一些变量的printf:
printf("string1 %d", string1); //output: 6356750
printf("string2 %d", string2); //output: 6356748
Run Code Online (Sandbox Code Playgroud)
为什么会有这样的数字?这个数字是什么意思?
我以为字符串数组中的数字代表ASCII码,但我认为我错了。
#include <stdio.h>
int main()
{
int i=10;
for (int i=1;i<=20;i++)
i++;
printf("%d",i);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我不明白,为什么此C程序总是只将输出打印为10,而不管在for循环中给出的条件如何。我试图通过在for循环中进行一些更改来执行它for (int i=1;i<=50;i++),但是它返回了相同的输出!这是什么错误?
我希望它为每种情况分配一个字符串值,并且还能够打印
int main()
{
int a;
char d;
printf("Input a Number\n");
scanf("%d",&a);
switch(a)
{
case 1:
d='One';
case 2:
d='Two';
case 3:
d='Three';
}
printf("You entered %s",d);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我期望输出:
input a number
1
you entered one
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用迭代器更改字符串中的字符。它会更改空格前的字符,但会删除该空格后的字符。
发生了什么事?
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s;
cout<<"Enter a string : ";
cin>>s;
for (auto it = s.begin(); it != s.end() && !isspace(*it);++it)
*it = toupper(*it);
cout<<s; // capitalize the current character
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出:
输入字符串:abc abc
ABC
当我尝试在Linux中使用G ++ 4.8编译程序时出现错误“未提供有效的预处理令牌”。并且在Solaris中使用CCSuntudio进行编译时没有错误。
在我的代码下面:
#include <iostream>
#define func(type1,varname1) \
cout << "ma var est "<<##varname1<<" et le type est "<<#type1; \
cout <<endl;
using namespace std;
int main() {
func("int", "area");
}
Run Code Online (Sandbox Code Playgroud)
它可以在CCSunStudio中完美运行,但不适用于G ++
hello.hxx:2:23: error: pasting "<<" and ""area"" does not give a valid preprocessing token
cout << "ma var est "<<##varname1<<" et le type est "<<#type1; \
^
hello.cxx:7:1: note: in expansion of macro ‘func’
func("int","area");
^
Run Code Online (Sandbox Code Playgroud)
谢谢你的帮助
我正在学习指针,但我被困在数组的动态分配上。
下面的代码提供了一个函数来查找具有最低值的元素。动态分配的数组作为参数传递给它。
#include <cstdlib>
#include <iostream>
using namespace std;
int findMin(int *arr, int n);
int main()
{
int *nums = new int[5];
int nums_size = sizeof(*nums);
cout << "Enter 5 numbers to find the minor:" << endl;
for(int i = 0; i < nums_size; i++)
cin >> nums[i];
cout << "The minor number is " << findMin(*nums, nums_size);
delete [] nums;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但它返回此错误:
error: invalid conversion from ‘int’ to ‘int*’ [-fpermissive]
Run Code Online (Sandbox Code Playgroud)
如何将该数组传递给函数?
只是出于好奇:如果我的数组由 5 个元素组成,为什么 for 循环允许我输入 4 …
我是 C 的新手,所以我开始玩一些代码。我的代码中有一个错误,因为我认为使用使用printf(pass)不安全,因为它可以覆盖该值,因此对用户不安全。我想知道printf(pass)我的代码不安全是否正确?另外,我怎样才能让用户在finally logged in不更改代码的情况下打印消息。有没有办法做到这一点?
我的代码:
#include <stdio.h>
char pass[100];
char getPass() {
int value = 'G';
int * j = & value;
fgets(pass, sizeof(pass), stdin);
printf("your entered pass is ");
printf(pass);
return (char)( * j);
}
void main() {
printf("enter the pass here ");
if (getPass() == 'K') {
printf("finally logged in\n");
exit(0);
} else {
printf("Wrong password\n");
exit(1);
}
}
Run Code Online (Sandbox Code Playgroud) 采取这个示例代码:
void CChristianLifeMinistryDiscussionsDlg::SetControlStates()
{
if (m_pEntry == nullptr)
return;
bool bClass1 = false, bClass2 = false;
m_pEntry->GetAuxiliaryClasses(bClass1, bClass2);
m_cbBrothersC1.EnableWindow(bClass1 ? TRUE : FALSE);
m_cbBrothersC2.EnableWindow(bClass2 ? TRUE : FALSE);
}
Run Code Online (Sandbox Code Playgroud)
EnableWindow需要一个类型为 的参数BOOL。我永远不清楚bool在这种情况下仅传递变量的值是否可以接受。
void __not_in_flash_func(adc_capture)(uint16_t *buf, size_t count) {
//....
}
Run Code Online (Sandbox Code Playgroud)
我不知道这个语法是什么意思。是__not_in_flas_func函数还是宏?它是如何装箱的?稍后在代码adc_capture中调用。
c ×5
c++ ×5
for-loop ×2
arrays ×1
boolean ×1
c-strings ×1
fgets ×1
function ×1
g++ ×1
heap-memory ×1
if-statement ×1
iterator ×1
mfc ×1
parameters ×1
preprocessor ×1
printf ×1
string ×1
syntax ×1
visual-c++ ×1