我有这个示例代码,用于将32位整数转换为ip地址.
#include <stdio.h>
int main()
{
unsigned int c ;
unsigned char* cptr = (unsigned char*)&c ;
while(1)
{
scanf("%d",&c) ;
printf("Integer value: %u\n",c);
printf("%u.%u.%u.%u \n",*cptr, *(cptr+1), *(cptr+2), *(cptr+3) );
}
}
Run Code Online (Sandbox Code Playgroud)
此代码为输入提供了错误的输出2249459722.但当我更换
scanf("%d",&c) ;Run Code Online (Sandbox Code Playgroud) 通过 scanf("%u",&c) ;Run Code Online (Sandbox Code Playgroud)
输出结果是正确的.
PS:我知道inet_ntop和inet_pton.
我期待的答案不仅仅是建议那些答案.
我意识到标题令人困惑,想不到更明确的方式来说出来.基本上,我在strtok循环中调用strtok循环,但是当内部strtok函数从runCommand返回时,我的第一个strtok循环停止.它只是退出循环,即使第一个分号后面还有其他参数.当我不调用runCommand()时,它按预期工作,并解析我用分号分隔的所有命令.
此代码的目标是解析由分号分隔的一行命令,然后解析命令和命令参数以便稍后进入execvp.这是我遇到麻烦的唯一部分.这里是:
void parseCommand(char *userLine)
{
if(strchr(userLine, ';'))
{
// Get first token
token = strtok(userLine, ";");
// Loop through all tokens
while(token != NULL)
{
// Make a copy
char *copy = malloc(strlen(token) + 1);
strcpy(copy, token);
runCommand(copy);
free(copy);
printf("process returned!\n");
token = strtok(NULL, ";");
}
}
}
void runCommand(char *token)
{
char *args[20];
char **command = args;
//Tokenize each command based on space
char *temp = strtok(token, " \n");
while (temp != NULL)
{
*command++ = temp;
temp …Run Code Online (Sandbox Code Playgroud) 我试图从Beginning C 5th Ed这本书编译一个示例程序.作者:Ivor Horton.试图在OSX上编译它,我从gcc获得以下输出:
$ gcc program6_07.c
program6_07.c:18:59: warning: format specifies type 'int' but the argument has
type 'size_t' (aka 'unsigned long') [-Wformat]
"Terminate input by entering an empty line:\n", str_len);
^~~~~~~
program6_07.c:23:5: warning: implicit declaration of function 'gets_s' is
invalid in C99 [-Wimplicit-function-declaration]
gets_s(buf, buf_len); // Read a lin...
^
program6_07.c:24:9: warning: implicit declaration of function 'strnlen_s' is
invalid in C99 [-Wimplicit-function-declaration]
if(!strnlen_s(buf, buf_len)) // Empty lin...
^
program6_07.c:27:8: warning: implicit declaration of function 'strcat_s' is
invalid in …Run Code Online (Sandbox Code Playgroud) 我按照这本书,无法编译这个例子.有什么建议?
1 #define __STDC_WANT_LIB_EXT1__ 1
2 #include <string.h>
3 #include <stdio.h>
4
5
6 int main(void)
7 {
8 char source[] = "Here we go...";
9 char destination[50];
10
11 if(strcpy_s(destination, sizeof(destination), source))
12 printf("An error occurred copying the string.n");
13
14
15 return 0;
16 }
Run Code Online (Sandbox Code Playgroud)
错误:
/tmp/ccc5KZDZ.o: In function `main':
test.c:(.text+0x48): undefined reference to `strcpy_s'
collect2: error: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud) 我正在尝试用 c 语言编写一个简单的边缘检测程序。我使用的是 Red Hat Enterprise Linux Server 7.7 (Maipo) 和 gcc 版本 4.8.5。
这是代码的开头:
#include <stdio.h>
#define size 200
int _tmain(int argc, _TCHAR* argv[])
{
char filein[size] = "./image.bmp";
FILE *fin;
fopen_s(&fin, filein, "rb");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我最初对 _TCHAR* 有很多问题,所以最终我用 char 替换了它,我不知道这以后是否会成为问题,但至少它编译并消除了这些错误。现在我收到隐式声明警告。我尝试通过添加其他#include 来修复它。
我尝试用以下方法修复它:
#include <stdio.h>
#include <errno.h>
#include <string.h>
#define size 200
int main(int argc, char* argv[])
{
char filein[size] = "./image.bmp";
FILE *fin;
fopen_s(&fin, filein, "rb");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但是,我仍然收到同样的警告,有人可以告诉我我做错了什么吗?
谢谢。
非常感谢,这有效!
#include <stdio.h>
#define size 200 …Run Code Online (Sandbox Code Playgroud) 有人可以向我解释为什么ints从用户那里获取的内容scanf()存储在8h分开的地址中,即使int我的64位机器的大小是4字节?它与内存中的对齐?
#include <stdio.h>
void main() {
int *a;
int i, n;
printf(" Input the number of elements to store in the array : ");
scanf("%d",&n);
printf(" Input %d number of elements in the array : \n",n);
printf("size of on int is %d\n", sizeof(i));
for(i=0;i<n;i++) {
printf(" element - %d : ",i+1);
printf("address of a is %p\n", &a+i);
scanf("%d",a+i);
}
return 0;
}
Input the number of elements to store in the array …Run Code Online (Sandbox Code Playgroud)