int main(){
char *c=(char*)malloc(4*sizeof(char));
*c='a';
c++;
*c='b';
c++;
*c='c';
c++;
*c='\0';
printf("%s",c);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
使用此代码,我可以打印每个单个字符(例如printf("%c",*(--c));),但是当我尝试使用printf("%s",c);该程序打印整个字符串时,什么都不打印!为什么会这样?
#include<stdio.h>
void main()
{
int a=100;
printf("%d",a);
int b=200; //Error
printf("%d",b);
}
Run Code Online (Sandbox Code Playgroud)
我知道在那里声明b导致错误,但我想知道为什么?
我还想让你们知道我在Windows上使用Turbo C++ 4.0编译了这个程序而且我得到的错误是"声明不允许在这里".
#include<stdio.h>
void m(int *p){
int i=0;
for(int i=0;i<5;i++)
printf("%d",p[i]);
}
int main(){
int a[5]={4,5,6};
m(a);
}
Run Code Online (Sandbox Code Playgroud)
我预计输出将是4 5 6 junkValue junkValue,但实际输出是4 5 6 0 0
我想提取一个子字符串,它包含除前两个之外的主字符串的所有字符。就像某个字符串有“0b1011”一样,我只需要“1011”。在给定的代码中,我需要字符串 c 只有“cdef”,而它包含“cdefabcdef”。
#include <stdio.h>
#include<string.h>
void main()
{
char a[6]="abcdef";
char c[4];
for(int i=2;i<6;i++)
c[i-2]=a[i];
printf("I need c: cdef\n");
printf("I get c:%s",c);
int k=strlen(c);
printf("\nlength of c: %d\n",k );
}
Run Code Online (Sandbox Code Playgroud) 为什么有a时set.count('a')输出?13
程序:
bool isAnagram(string s, string t) {
unordered_set<char> set;
for(int i=0; i<s.size(); i++){
set.insert(s[i]);
}
cout << endl << set.count('a') << endl;
return false;
}
Run Code Online (Sandbox Code Playgroud)
输入:
s = 'anagram'
Run Code Online (Sandbox Code Playgroud)
输出:
1
Run Code Online (Sandbox Code Playgroud) 我正在 Visual Studio 中编写我的第一个 c 程序,并且由于格式说明符而出现非常基本的错误。
#include <stdio.h>
int main()
{
int x,y;
int area = x*y;
printf("Enter the value of x \n");
scanf("%d", &x);
printf("Enter the value of y \n");
scanf("%d", &y);
printf("Area of the rectangle is %d\n", area);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
error: Enter the value of x
2 Enter the value of y
1 矩形的面积是-293795784 // 这是错误。我期待输出 2,即 x 和 y 的乘积 不确定为什么它采用值的地址而不是值。
我有成功的理解当有定义然后两个xor表达式时它意味着什么.这定义的内容是什么?
我尝试发送x = 8,y = 7,结果是x = 15和y = 8为什么它的开心?
这是程序:
#define FUNC(a,b) a^=b; b ^=a;
int main(){
int x=8,y= 7;
FUNC(x,y);
printf("%d %d\n",x, y);
}
Run Code Online (Sandbox Code Playgroud) 我在C++中有两个类
Class A
{
public:
int v;
}
Class B
{
public :
void calculation ();
}
void B::calculation ()
{
int i;
for (i= 1; i < object_a.v ;i++)
{
//some body
}
}
//now I have created an object for class A and for that object I have
//got some value for "v"
int main ()
{
A object_a();
cout << "The value of variable v in class A is" << object_a.v << ;
}
Run Code Online (Sandbox Code Playgroud)
对于上面的示例代码,我尝试使用在类B中定义的另一个函数中使用object_a检索的变量.但是它会抛出未声明的错误.有人可以帮我这里怎么去吗?
PS:我在B类中编译函数"calculation()"时遇到错误