小编Jen*_*edt的帖子

常量的默认类型

我写的时候:

if ((1/3) > 0) ...
Run Code Online (Sandbox Code Playgroud)

我是否需要将其中一个操作数转换为(float)以使此条件成立?或者什么是默认的变量类型C正在使用?

如果我愿意写:

if ((1.0/3) > 0) ...
Run Code Online (Sandbox Code Playgroud)

情况现在已经改变了,因为编译器注意到其中一个操作数必须是浮点数?

c c99

0
推荐指数
2
解决办法
252
查看次数

短的无符号数据中的字节分配

请检查以下程序

#include <stdio.h>
#include<stdlib.h>
int main()
{
        char *Date= NULL;
        unsigned short y=2013;
        Date =  malloc(3);
        sprintf((char *)Date,"%hu",y);  
        printf("%d %d %d %d %d \n",Date[0],Date[1],Date[2],Date[3],Date[4]);

        printf("%s %d %d",Date,strlen(Date),sizeof(y));
}

output:
50 48 49 51 0
2013 4 2
Run Code Online (Sandbox Code Playgroud)

我如何得到字符串长度4而不是2,因为我将一个短整数值放入内存,所以它应该占用2字节的内存.但它是如何占用4字节.

每个字节如何从输入获得2 0 1 3,而在一个字节中为20,在另一个字节中为13.

我想把20到1个字节和13个放到另一个字节.怎么做.请告诉我

请给出一些答案.

c

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

为什么我的makefile中的自动变量找不到任何目标?

我写了这个问候世界hello.c:

#include <stdio.h>

int main() {
  printf("Hello, World!\n");
  exit( 0 );
}
Run Code Online (Sandbox Code Playgroud)

Makefile是:

%: %.c
Run Code Online (Sandbox Code Playgroud)

当我运行时,make我会收到此错误:make: *** No targets. Stop.

makefile gnu-make

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

无法编译C++代码:从"int"到"node*"的无效转换

#include<iostream>

using namespace std;

struct node
{
    int data;
    node* next;
};

void pushList(struct node **head_ref, int element)
{
    struct node *temp = (struct node*)malloc(sizeof(struct node));
    temp->data = element;
    temp->next = *head_ref;
    *head_ref = temp;
}

void printList(struct node* node)
{
    while (node != NULL)
    {
        cout << node->data << endl;
        node = node->next;
    }
}

struct node* thirdLastElement(struct node *head)
{
    struct node *slow = head;
    struct node *fast = head;

    if (head == NULL || head->next == NULL) …
Run Code Online (Sandbox Code Playgroud)

c++ data-structures

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

如何为橙色调文本?

我正在尝试找到如何用橙色为文本着色.

我试过这个:

SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED | BACKGROUND_GREEN);
Run Code Online (Sandbox Code Playgroud)

但它会给我一个红色的绿色文字.

是否有可能将颜色变为橙色?我尝试了在互联网上找到的不同代码,但没有一个给我橙色.

谢谢您的帮助.

c windows

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

在C中传递结构

当我将struct指针传递给函数时,我希望在函数结束后保留​​对指针所做的更改.我不知道我做错了什么.

void webSocketHandler(struct libwebsocket_context* context)
{    
    /* some code */
    context = libwebsocket_create_context(&info);
}

sint32 s32_WSER_Init(sint32* configDataPoolIndexes, struct libwebsocket_context* context)
{
   /* some code */
  webSocketHandler(configDataPoolIndexes, context);
}
Run Code Online (Sandbox Code Playgroud)

问题是在创建它之后context不在NULL内部webSocketHandler,但如果我s32_WSER_Init在调用处理程序后尝试在内部使用它,我会得到NULL.

提前致谢.

c pointers structure function

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

给移动物体指明方向

我想在SDL中创建一个塔防游戏.在开始这个项目之前,我会尝试编写游戏时需要做的所有事情.在我正在进行的测试中,有一个塔(静态物体),在其范围内的目标(移动物体),以及从炮塔发射到目标的射击(移动物体).我没有做的是找到一种方法让'射击'物体成为一个方向.通过射击物体,我的意思是当目标在范围内时由塔发射的物体.此外,无论方向如何,射击应始终具有相同的速度,这禁止使用配方dirx = x2 - x1.

射击是定义如下的结构:

typedef struct shoot
{
    SDL_Surface *img; // Visual representation of the shoot object.
    SDL_Rect pos;     // Position of the object, it's a structure containing
                      // coordinates x and y (these are of type int).
    int index;
    float dirx;       // dirx and diry are the movement done by the shoots object in
                      // 1 frame, so that for each frame, the object shoot is moved dirx
                      // pixels on the axis …
Run Code Online (Sandbox Code Playgroud)

math sdl

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

错误输入的scanf设置值

我写了一个简单的代码,我有一个问题,这是我的代码:

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
  int first = 0;   
  int second = 0;

  printf("please enter two integers to determine their relation:  \n");
  scanf("%d %d",&first , &second);
  if ( first == second)
  {
    printf("%d and %d are equal\n", first ,second);
  }

  else if (first > second) 
  {
    printf("%d is bigger than %d \n" ,first ,second);
  }
  else if (first < second)
  {
    printf("%d is smaller than %d \n" ,first ,second);   
  }
  else
  {
    printf("something is wrong \n");`
  } …
Run Code Online (Sandbox Code Playgroud)

c

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

为什么加入一个线程最终会调用多个线程?

我正在学习有关线程和Linux上C语言中的线程编程的知识。我了解的是,加入线程只是调用线程并等待其执行,就像等待子进程运行一样。但是不知道为什么在尝试加入一个线程时最终会调用两个线程!

#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>

void *start1()
{
    printf("Hello from Thread 1\n");

}

void *start2()
{
    printf("Hello from Thread 2\n");
}

void main()
{
    pthread_t t1,t2;

    pthread_create(&t1,NULL,start1,NULL);
    pthread_create(&t2,NULL,start2,NULL);
    pthread_join(t1,NULL); 
}
Run Code Online (Sandbox Code Playgroud)

当我运行代码时,输​​出如下:

[root@localhost]# ./a.out 
Hello from Thread 1
Hello from Thread 2
Run Code Online (Sandbox Code Playgroud)

我希望它仅调用start1的代码。

multithreading pthreads process

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

键入一个指向int的void指针,内存分配不足

我正在初始化一个带有1个字节内存的void指针,并将其类型转换为int指针,并取消引用它,给它一个值3(需要4个字节),但运行正常.这不应该导致错误或导致像OOM这样的运行时异常吗?

void* record = malloc(1);
int i=3;
*((int*)record) = 3;
Run Code Online (Sandbox Code Playgroud)

c pointers void-pointers

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

用#ifdef或类似的东西替换开关/外壳

我试图switch/case通过其他工具替换结构做同样的事情,但具有更好的性能(更少的执行时间...),我想到的#ifdef方法,但我不知道如何在这种情况下使用它:

float k_function(float *x,float *y,struct svm_model model)
{
    int i;
    float sum=0.0;
    switch(model.model_kernel_type)  
    {
    case LINEAR :
        return result1;
    case POLY :
        return result2;
    case RBF :
        return result3;
    case SIGMOID :
        return result4;
    default :
        return 0;
    }
}
Run Code Online (Sandbox Code Playgroud)

PS:

typedef   enum   kernel_type   {LINEAR, POLY, RBF, SIGMOID};
Run Code Online (Sandbox Code Playgroud)

c conditional-compilation switch-statement

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

在C中,从main调用一个函数

在C中,我试图从main调用函数printSum.但主要功能是不调用printSum,它只打印出"嗨!" 这是来自main的打印声明.我不确定为什么没有调用printSum.谢谢.

码:

int main(void){

  void printSum(void);
  printf("Hi!\n");

  return 0;
}

void printSum (void){
  printf("Please give two integers\n");
    int x,y;
  scanf("%d %d", &x,&y);
  printf("%d + %d is %d\n",x,y,x+y);
}
Run Code Online (Sandbox Code Playgroud)

山姆

c function

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