小编438*_*427的帖子

动态分配的字符串填充了字符,结果为空字符串C.

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);该程序打印整个字符串时,什么都不打印!为什么会这样?

c memory string malloc

1
推荐指数
1
解决办法
514
查看次数

为什么在main()函数的起始行之后声明变量会导致错误?

#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编译了这个程序而且我得到的错误是"声明不允许在这里".

c variables

0
推荐指数
1
解决办法
59
查看次数

为什么这样打印4 5 6 0 0而不是4 5 6 junk_value junk_value

#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

c pointers

0
推荐指数
1
解决办法
68
查看次数

如何提取除初始两个字符以外的所有字符的子字符串?

我想提取一个子字符串,它包含除前两个之外的主字符串的所有字符。就像某个字符串有“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)

c string

0
推荐指数
1
解决办法
62
查看次数

unordered_set count 函数返回错误值

为什么有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)

c++ unordered-map unordered-set

0
推荐指数
1
解决办法
127
查看次数

由于 %,printf 中的 c 程序出错

我正在 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 的乘积 不确定为什么它采用值的地址而不是值。

c visual-studio-2010

0
推荐指数
1
解决办法
85
查看次数

有没有办法将两行连接成一个语句?

喜欢

pri
ntf("hi");
Run Code Online (Sandbox Code Playgroud)

我可以在宏中使用 \,但我认为这在这里不起作用:'(

c

0
推荐指数
1
解决办法
91
查看次数

#define FUNC(x,y)x = ^ y; Y 1 X; 在c

我有成功的理解当有定义然后两个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

-1
推荐指数
1
解决办法
788
查看次数

如何在另一个类中使用类对象变量

我在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()"时遇到错误

c++ graph data-structures

-2
推荐指数
1
解决办法
46
查看次数