每当我在书籍,手册页和网站中查看真实代码或示例套接字代码时,我几乎总会看到类似下面的内容:
struct sockaddr_in foo;
memset(&foo, 0, sizeof foo);
/* or bzero(), which POSIX marks as LEGACY, and is not in standard C */
foo.sin_port = htons(42);
Run Code Online (Sandbox Code Playgroud)
代替:
struct sockaddr_in foo = { 0 };
/* if at least one member is initialized, all others are set to
zero (as though they had static storage duration) as per
ISO/IEC 9899:1999 6.7.8 Initialization */
foo.sin_port = htons(42);
Run Code Online (Sandbox Code Playgroud)
要么:
struct sockaddr_in foo = { .sin_port = htons(42) }; /* New in C99 */ …Run Code Online (Sandbox Code Playgroud) 我正在使用C++非托管项目.
我需要知道如何使用这样的字符串"一些数据加密"并得到一个byte []数组,我将用它作为加密的源.
在C#我做
for (int i = 0; i < text.Length; i++)
buffer[i] = (byte)text[i];
Run Code Online (Sandbox Code Playgroud)
我需要知道的是如何使用非托管C++.
谢谢!
我有一个头文件port.h,port.c和我的main.c
我收到以下错误:'ports'使用未定义的struct'port_t'
我想,因为我在.h文件中声明了结构,并且.c文件中的实际结构是可以的.
我需要有前向声明,因为我想在port.c文件中隐藏一些数据.
在我的port.h中,我有以下内容:
/* port.h */
struct port_t;
Run Code Online (Sandbox Code Playgroud)
port.c:
/* port.c */
#include "port.h"
struct port_t
{
unsigned int port_id;
char name;
};
Run Code Online (Sandbox Code Playgroud)
main.c中:
/* main.c */
#include <stdio.h>
#include "port.h"
int main(void)
{
struct port_t ports;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
非常感谢任何建议,
以下程序具有未定义的行为:
#include <stdio.h>
int main(void)
{
unsigned int x = -100; // This is fine, becomes UINT_MAX - 100
printf("%d\n", x); // This is undefined behavior.
return 0;
}
Run Code Online (Sandbox Code Playgroud)
C99 7.19.6.1p8状态%d需要一个int参数.
C99 7.19.6.1p9声明"如果任何参数不是相应转换规范的正确类型,则行为 未定义."
但是,gcc -Wformat(包含在内-Wall)不会抱怨上述程序,为什么?这是一个错误,还是故意遗漏?
从gcc手册页:
-Wformat
Run Code Online (Sandbox Code Playgroud)
检查电话"printf"和"scanf"等,以确保提供参数指定适当的格式字符串类型,并在格式字符串指定的转换意义
关于StackOverflow的第一个问题......
有没有人知道通过CodeBlocks查看语言的参考/文档手册的方法?专门针对C/C++.
示例:
假设我要查找strncpy()的引用.在一个非常古老的Borland系统(我们在学校使用)中,我会写下这个单词并在其上单击鼠标,从而将其作为参考.如果可以以某种方式浏览手册,那也很好.
我是否完全忽略了这一点(也在搜索谷歌),还是缺少了?如果它丢失了,那么在没有在线的情况下搜索C和/或参考手册的最快方法是什么?
问候!
atoi()给了我这个错误:
error C2664: 'atoi' : cannot convert parameter 1 from 'char' to 'const char *'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
Run Code Online (Sandbox Code Playgroud)
从这一行:int pid = atoi(token.at(0)); 其中token是一个向量
我怎么能绕过这个?
我的Objective-C代码有问题.我试图打印出从我的"Person"类创建的对象的所有细节,但是在NSLog方法中不会出现名字和姓氏.它们被空格所取代.
Person.h:http ://pastebin.com/mzWurkUL Person.m:http://pastebin.com/JNSi39aw
这是我的主要源文件:
#import <Foundation/Foundation.h>
#import "Person.h"
int main (int argc, const char * argv[])
{
Person *bobby = [[Person alloc] init];
[bobby setFirstName:@"Bobby"];
[bobby setLastName:@"Flay"];
[bobby setAge:34];
[bobby setWeight:169];
NSLog(@"%s %s is %d years old and weighs %d pounds.",
[bobby first_name],
[bobby last_name],
[bobby age],
[bobby weight]);
return 0;
}
Run Code Online (Sandbox Code Playgroud) c ×4
c++ ×3
atoi ×1
codeblocks ×1
declaration ×1
forward ×1
gcc ×1
gcc-warning ×1
memset ×1
objective-c ×1
oop ×1
sockets ×1
string ×1
struct ×1