小编alk*_*alk的帖子

C语言中__uuidof的替代

我正在使用一个使用DirectX的C项目,但我遇到了一个问题.某些DX调用需要一个IID对象,通常用__uuidof.这需要做的一件事是创建一个RenderTargetView.DirectX示例/教程执行此操作:

ID3D11Texture2D* pBackBuffer = NULL;
hr = g_pSwapChain->GetBuffer( 0, __uuidof( ID3D11Texture2D ), ( LPVOID* )&pBackBuffer );
Run Code Online (Sandbox Code Playgroud)

当我尝试调用__uuidof我的C代码时,我收到编译器错误:Error 19 error C4233: nonstandard extension used : '__uuidof' keyword only supported in C++, not C.DirectX有一个C接口,所以我想必须有办法做到这一点,但我不知道它会是什么.谁知道?

c directx uuid guid visual-studio

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

没有出界错误

我在C中有这个代码,它接受了一堆chars

#include<stdio.h> 
# define NEWLINE '\n'
int main()
{

char c;
char str[6];
int i = 0;
while( ((c = getchar()) != NEWLINE))
{
        str[i] = c;
        ++i;
        printf("%d\n", i);
}

return 0;
}
Run Code Online (Sandbox Code Playgroud)

输入是:testtesttest

输出:1 2 3 4 5 6 7 8 117 118 119 120

我的问题是:

  1. 虽然我明显超出了阵列的容量,为什么我不会出现超出界限(分段错误)的异常?

  2. 为什么输出中的数字会突然跳到很大的数字?

我在C++中试过这个并且得到了相同的行为.请问有谁可以解释一下这是什么原因?

c indexoutofboundsexception

12
推荐指数
2
解决办法
2万
查看次数

Makefile:检测到时钟偏差

我的问题是每当我尝试使用Makefile进行编译时,我得到以下内容:

make: Warning: File `Board.c' has modification time 1.3e+03 s in the future
gcc -Wall -c -Wvla -lm Board.c -o Board.o
gcc -Wall -c -Wvla -lm PlayBoard.c -o PlayBoard.o
gcc -lm ErrorHandle.o Board.o PlayBoard.o -g -o PlayBoard
make: warning:  Clock skew detected.  Your build may be incomplete.
Run Code Online (Sandbox Code Playgroud)

我的Makefile是:

CC = gcc
FLAGS = -Wall -c -Wvla

PlayBoard: ErrorHandle.o Board.o PlayBoard.o
    $(CC) -lm ErrorHandle.o Board.o PlayBoard.o -g -o $@

PlayBoard.o: PlayBoard.c Board.o
    $(CC) $(FLAGS) -lm PlayBoard.c -o $@

Board.o : ErrorHandle.o …
Run Code Online (Sandbox Code Playgroud)

makefile clock skew

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

如何获取原始命令行参数

我需要将命令行参数从A.exe传递给B.exe.如果A.exe与多args一样

A.exe -a="a" -b="b"'

我可以使用

BeginProcess("B.exe", **args!**)

开始B.exe.我怎样才能获得原始命令行参数

'-a="a" -b="b"'

c c++ windows

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

命令行参数,读取文件

如果我进入命令行C:myprogram myfile.txt

如何在程序中使用myfile.我是否必须扫描它或是否有任意方式访问它.

我的问题是如何在我的程序中使用myfile.txt.

int
main(){
    /* So in this area how do I access the myfile.txt 
    to then be able to read from it./*
Run Code Online (Sandbox Code Playgroud)

c arguments file command-line-arguments

11
推荐指数
1
解决办法
7万
查看次数

printf()打印整个数组

假设我的C程序中有以下代码:

#include <stdio.h>

void PrintSomeMessage( char *p );

int main(int argc, char *argv[]) {
    char arr[10] = "hello";
    PrintSomeMessage(&arr[0]);
    return 0;   
}

void PrintSomeMessage(char *p)
{
    printf("p: %s",p);
}
Run Code Online (Sandbox Code Playgroud)

为什么输出这个单词"hello"而不是单个字符"h"?

但我明白,如果我"%c"在格式化程序中添加一个,它只会打印一个字母.但是,此地址中每个字母的内存地址不同.请有人向我解释一下吗?

c arrays printf char

11
推荐指数
2
解决办法
12万
查看次数

检查当前线程是否是主线程

我怎样才能检查当前线程是否是linux上的主线程?看起来像gettid()只返回一个pid,但似乎linux不保证带有main()的线程总是有一个const和统一的pid.

这样做的原因是我有一个自动并行化,我想确保在已经由pthread_create()创建的线程上运行的函数中不调用pthread_create().

c c++ linux multithreading pthreads

11
推荐指数
2
解决办法
5405
查看次数

pthread_exit,pthread_join和pthread_detach之间的区别

我是pthreads的新手,我想知道确切的差异是什么.

pthread_exit退出一个线程.因此pthread_join将返回; 但是,分离的作用与pthread_join有什么不同?

例如,我创建一个线程,并说让线程完成,我想完全终止线程,以便我以后可以重新创建它.什么是更好的使用.pthread_join还是pthread_detach?

所以执行的顺序是

pthread_exit();
pthread_join(); or pthread_detach();
Run Code Online (Sandbox Code Playgroud)

c linux multithreading pthreads

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

如何在符合C标准的情况下一般地转换指针的地址

通常使用隐式函数返回void*转换为分配指定指针,就像malloc()一样:

void *malloc(size_t size);
int *pi = malloc(sizeof *pi);
Run Code Online (Sandbox Code Playgroud)

我希望在传递目标指针的地址时执行相同的赋值,而不从函数内部(不在其体内,也不在参数中)显式地转换其类型.

以下代码似乎实现了这一点.

  1. 我想知道代码是否完全符合(任何)C标准.
  2. 如果它不符合,我想知道是否有可能在符合(任何)C标准的情况下达到我的要求.

.

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

int allocate_memory(void *p, size_t s) {
    void *pv;
    if ( ( pv = malloc(s) ) == NULL ) {
        fprintf(stderr, "Error: malloc();");
        return -1;
    }
    printf("pv: %p;\n", pv);
    *((void **) p) = pv;
    return 0;
}

int main(void) {
    int *pi = NULL;
    allocate_memory(&pi, sizeof *pi);
    printf("pi: %p;\n", (void *) pi);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

结果:

pv: 0x800103a8;
pi: 0x800103a8;
Run Code Online (Sandbox Code Playgroud)

c pointers strict-aliasing

11
推荐指数
2
解决办法
554
查看次数

将一个指针内容复制到另一个

我以为我读过某个地方,当使用指针并且我们想要将一个内容复制到另一个时,有两个选项:

  • 使用memcpy或
  • 只是给他们分配=?

然而在下面的例子中,我只是通过为两个指针分配内存来测试它,然后分配第二个,先改变..然后我的第二个指针的输入也在改变.我做错了什么:/.

typedef struct {

    int a;
    int b;
    int c;
} my_struct;


int main(int argc, char** argv) {

    my_struct* first = malloc(sizeof(my_struct));   
    first->a = 100; first->b = 101; first->c = 1000;

    my_struct* bb = malloc(sizeof(my_struct));  

    printf("first %d %d %d\n", first->a, first->b, first->c);
    bb = first;
    printf("second %d %d %d\n", bb->a, first->b, bb->c);


    first->a = 55; first->b = 55; first->c = 89;
    printf("second %d %d %d\n", bb->a, first->b, bb->c);
}
Run Code Online (Sandbox Code Playgroud)

c pointers

11
推荐指数
3
解决办法
4万
查看次数