我想检查文件是目录,链接还是仅仅是常规文件.我遍历目录并将每个文件保存为struct dirent *.我试图传递d_ino给S_ISDIR(m),, S_ISLINK(m)或者S_ISREG(m)无论文件如何,我都不会得到积极的结果.所以我的问题是:我怎么使用S_ISDIR(m)带struct dirent?
好的,所以我试着这样做
int b;
char x = 'a';
//Case 1
b = static_cast<int>(x);
std::cout<<"B is : "<<b<<std::endl;
//Case 2
b = *(int*)&x;
std::cout<<"B is changed as :: "<< b <<std::endl;
Run Code Online (Sandbox Code Playgroud)
现在我知道在第2种情况下,第一个字节x被重新解释为认为它是一个整数并且位模式被复制到b其中给出了一些垃圾,而在情况1中它只是将值转换char为int.
除此之外,这两者有什么不同吗?
我正在上C课,我不是很擅长,所以我想问你,我怎么解决这个问题:"Id返回1退出状态",我一直在努力解决这个问题.虽然如此,所以我非常感谢你的帮助.
#include <stdio.h>
#include<conio.h>
#include<windows.h>
int main()
{
int P, N, NP=0;
printf("Introduzca en nombre del producto:\n");
scanf("%f", &N);
printf("Introduzca en precio del producto:\n");
scanf("%f", &P);
if (P <= 1500)
NP=P*1.11;
else
NP=P*1.08;
printf("El producto %d cuesta %d", NP, N);
getche();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
完整的错误列表是:
Permission denied
Id returned 1 exit status
Run Code Online (Sandbox Code Playgroud) 我正在尝试返回浮点值但是当我打印出来时,我得到0.0.
float floating = 4.5;
void* function(){
void* test = &floating;
return test;
}
int _tmain(int argc, _TCHAR* argv[]){
printf("%f\n", test());
return 0;
}
Run Code Online (Sandbox Code Playgroud)
关于它为什么不打印4.5的任何想法?对不起,如果这是一个菜鸟问题,我还是比较新的.
我想首先说 - 我对"幕后"发生的事情感兴趣导致这个问题,因为我正在盲目修复代码.也许C++标准或我不熟悉的东西决定了它的原因:-)
无论如何...
我试图将3个数组传递A, B, C给一个函数,该函数将合并A并B存储结果C.
功能声明: void merge(int* a, int* b, int* c)
通过main():
int A[] = {1,2,3,4};
int B[] = {6,7,8,9};
int* C; //this could be wrong, btw!
merge(A, B, C);
Run Code Online (Sandbox Code Playgroud)
问题1.
奇怪的是main(),如果我打印结果sizeof(A)/sizeof(A[0]),它给出了数组的"长度"的正确结果4- 和B的相同的东西.但是当我将数组传递给函数时,我尝试通过相同的方法再次计算大小,但我得到2两个数组的结果.第一行merge():
void merge(int* a, int* b, int* c)
{
int sizeA = sizeof(a)/sizeof(a[0]);
int sizeB = sizeof(b)/sizeof(b[0]);
int totalsize = sizeA + …Run Code Online (Sandbox Code Playgroud) 为什么以下代码在g ++下编译时没有任何警告或错误?我看到的问题是第一行中定义的变量x可以在if范围内访问,但尽管它再次被重新定义.
int main() {
int x = 5;
std::cout << x;
if (true) {
int x = 6;
std::cout << x;
}
}
Run Code Online (Sandbox Code Playgroud) 使用exec命令运行程序有什么区别?
例如,如果我制作了如下的脚本文件.
#script1
python test.py
#script2
exec python test.py
Run Code Online (Sandbox Code Playgroud)
两者似乎都返回相同的结果.
它们是等价的吗?
我正在尝试使用该函数创建 1000 个线程pthread_create()。
这是我正在使用的声明:
for (int i=0 ; i <1000; i++)
{
retValue = pthread_create(&threadId, NULL, simplethreadFunction, NULL);
}
Run Code Online (Sandbox Code Playgroud)
每次运行这个 for 循环时都会创建一个新线程吗?
这是一件简单的事情。但我无法理解它。
以下程序(什么都不做)运行正常.
#include <stdio.h>
int main(int argc, char *argv[])
{
char *current_string = NULL;
fgets(current_string, 16, stdin);
return(0);
}
Run Code Online (Sandbox Code Playgroud)
但是,当我在GDB下运行它时,该fgets调用会触发分段错误.
Program received signal SIGSEGV, Segmentation fault.
0x000000332ec67ee0 in _IO_getline_info_internal () from /lib64/libc.so.6
Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.149.el6_6.9.x86_64
(gdb) backtrace
#0 0x000000332ec67ee0 in _IO_getline_info_internal () from /lib64/libc.so.6
#1 0x000000332ec66d29 in fgets () from /lib64/libc.so.6
#2 0x0000000000400536 in main (argc=1, argv=0x7fffffffd4e8) at test.c:7
Run Code Online (Sandbox Code Playgroud)
据我所知,我一直在关注该fgets函数的正确用法,我不明白发生了什么.我做错了什么?
我正在用gcc编译-gRHEL上的选项(用于调试信息).
我有一个 C 程序,比如说hello_world。main 函数返回一个int. 我可以在 shell 脚本中访问和使用这个返回值吗?
这是C代码。我编写了一个非常简化的程序版本。实际代码是 1.2K 行。
/*hello_world.c*/
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int i = 0;
if (argc == 2) {
i = atoi(argv[1]);
printf("Hello World - %d\n", i);
return 0;
}
else return -1;
}
Run Code Online (Sandbox Code Playgroud)
这是运行上述代码编译后生成的可执行文件的bash脚本。我正在使用 GCC 4.1.2 并使用编译gcc -o hello_world hello_world.c
#!/bin/bash
ret=hello_world 31 # this gives a `command not found` error
if [ ret -eq 0 ]; then
echo "success"
fi
Run Code Online (Sandbox Code Playgroud)
有什么方法可以访问脚本的返回值吗?