我正在通过以下链接重写应用程序:http: //www.raywenderlich.com/12910/how-to-make-a-simple-playing-card-game-with-multiplayer-and-bluetooth-part-3
而且我坚持NSData和那些有关的东西bytes!
0x64代表什么类型?
typedef enum
{
PacketTypeSignInRequest = 0x64, // server to client
PacketTypeSignInResponse, // client to server
...
}
PacketType;
Run Code Online (Sandbox Code Playgroud)
我的枚举应该迅速采用哪种类型?
我遇到的主要问题是:
- (void)rw_appendInt32:(int)value
{
value = htonl(value);
[self appendBytes:&value length:4];
}
- (void)rw_appendInt16:(short)value
{
value = htons(value);
[self appendBytes:&value length:2];
}
- (void)rw_appendInt8:(char)value
{
[self appendBytes:&value length:1];
}
- (void)rw_appendString:(NSString *)string
{
const char *cString = [string UTF8String];
[self appendBytes:cString length:strlen(cString) + 1];
}
Run Code Online (Sandbox Code Playgroud)
我不知道应该如何写这篇文章NSData.
最后但并非最不重要的是,我如何在swift中传递这个'SNAP',因为字符与swift中的字符串几乎相同?
[data rw_appendInt32:'SNAP']; // …Run Code Online (Sandbox Code Playgroud) 我有一个通过 C FFI(通过 winapi-rs)使用 Windows API 的程序。其中一个函数需要一个指向字符串的指针作为输出参数。该函数将其结果存储到此字符串中。我正在WideCString为此字符串使用类型变量。
我可以“只是”将一个可变引用传递给一个字符串引用到这个函数中(在一个不安全的块内),还是我应该使用类似的功能.into_raw()并且.from_raw()也将变量的所有权移动到 C 函数?
两个版本都可以编译和工作,但我想知道我是否购买了直接方式的任何缺点。
这是我的代码中使用.into_raw和的相关行.from_raw。
let mut widestr: WideCString = WideCString::from_str("test").unwrap(); //this is the string where the result should be stored
let mut security_descriptor_ptr: winnt::LPWSTR = widestr.into_raw();
let rtrn3 = unsafe {
advapi32::ConvertSecurityDescriptorToStringSecurityDescriptorW(sd_buffer.as_mut_ptr() as *mut std::os::raw::c_void,
1,
winnt::DACL_SECURITY_INFORMATION,
&mut security_descriptor_ptr,
ptr::null_mut())
};
if rtrn3 == 0 {
match IOError::last_os_error().raw_os_error() {
Some(1008) => println!("Need to fix this errror in get_acl_of_file."), // Do nothing. No …Run Code Online (Sandbox Code Playgroud) 使用CString.Format(),我传给它一个std::map返回std::string给定的时候int.
所以:
CString cStr;
cStr.Format("%s", IntToStdStringMap[1]);
Run Code Online (Sandbox Code Playgroud)
其中,IntToStdStringMap[1]返回一些字符串,我们会说"你好,世界!".问题是每次都不会崩溃.最终,我将收到访问冲突.
为什么会这样?
请记住,将代码更改为以下内容:
CString cStr;
cStr.Format("%s", IntToStdStringMap[1].c_str());
Run Code Online (Sandbox Code Playgroud)
缓解了这个问题.
有任何想法吗?
如标题所示,该问题是我在密码功能的特定部分执行程序时出错。实际上,这是一个基本的密码功能,在Turbo C ++中正常工作,但是在Visual C ++中,此错误到来
void user::password()
{
char any_key, ch;
string pass;
system("CLS");
cout << "\n\n\n\n\n\n\n\n\t\t\t\t*****************\n\t\t\t\t*ENTER
PASSWORD:*\n\t\t\t\t*****************\n\t\t\t\t";
start:
getline(cin,pass);
if (strcmp(pass, "sha") == 0) //this is where the error is!*
{
cout << "\n\n\t\t\t\t ACCESS GRANTED!!";
cout << "\n\t\t\t PRESS ANY KEY TO REDIRECT TO HOME PAGE";
cin >> any_key;
}
else
{
cout << "\n\t\t\t\t ACCESS DENIED :(,RETRY AGAIN!!\n\t\t\t\t";
goto start;
}
system("CLS");
}
Run Code Online (Sandbox Code Playgroud) 我有一个 C 程序,它已经接受了一个参数,将它存储到一个缓冲区中,然后将它传递给一个写函数:
main(int argc, char** argv) {
...
char buff[1000];
strcpy(buff, argv[1]);
write(server_socket, buff, sizeof(buff));
...
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如果我执行我的代码:./testCode "equinox",equinox将存储在缓冲区中,然后缓冲区将传递给write()函数。在这种情况下,一切都会按预期工作。
有没有办法跳过将参数存储在缓冲区中的部分,然后将其传递给 write()?
如果我做这样的事情是否正确:
write(server_socket, argv[1], sizeof(argv[1]));
Run Code Online (Sandbox Code Playgroud)
我什至不确定这是否会按预期工作。
我正在为一个大学项目做 ac 程序。我有下一个代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
...
int main(int argc, char const *argv[])
{
...
char * resolve = 0;
sprintf(resolve, "%s,%s\n", HTTP_HEADER, ERR_404);
printf("%s", resolve);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我在 sprintf 的行中遇到分段错误,以某种方式获得这项工作。PD:HTTP_HEADER 和 ERR_404 是定义的字符串。
我有以下两行代码:
const char* skimfile = argv[3];
cout << "skimfile = " << skimfile << endl;
Run Code Online (Sandbox Code Playgroud)
我知道上面两行代码有效,但我不知道为什么。如果我们想打印出指针指向的值,我们不应该使用*skimfile? 上面的代码怎么只skimfile用来访问指针指向的值skimfile?const在指针的声明前面是否会使这种情况有所不同?
非常感谢您的帮助!对此,我真的非常感激!
我正在编写一个 C 程序,该程序将连接所有行(\n包括“ ”),同时将指针保存到最终字符串的最后一个字符。但是,我没有得到我预期的结果。可能是什么问题呢?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Node {
struct Node *next;
struct Node *prev;
};
struct Node *CreateNewNode() {
struct Node *newNode = malloc(sizeof(struct Node));
return newNode;
}
struct PieceTable {
char *buffer;
char *ptr_to_last_character;
} PT;
void strconcatenate(char *source) {
size_t source_len = strlen(source);
size_t buffer_len = strlen(PT.buffer);
PT.buffer = realloc(PT.buffer, buffer_len + source_len + 1);
while (*source)
*PT.ptr_to_last_character++ = *source++;
*PT.ptr_to_last_character = '\0';
}
int main(int argc, char *argv[]) {
char …Run Code Online (Sandbox Code Playgroud) c pointers c-strings string-concatenation function-definition
我是 C 的新手,并试图使用 char ** 创建一个数组,但我在打印它时遇到了问题。有没有办法打印出来,或者我不应该用这种方式来创建一个字符串数组?
int main()
{
char **a = {"abc", "ddd", "ccc", "aaa"};
for (size_t i = 0; i < 4; i++){
printf("%s\n", a+i); // only print out "abc" correctly
// printf("%s\n", *(a+i)); doesn't work
// printf("%s\n", a[i]); doesn't work
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
举例来说,我以以下方式声明了一个所有值都设置为零的 char 数组:
char array[4] = {0};
Run Code Online (Sandbox Code Playgroud)
如果我给它赋值,例如:
array[0] = 'A';
array[1] = 'B';
array[2] = 'C';
Run Code Online (Sandbox Code Playgroud)
我是否需要像这样空终止数组?:
array[3] = '\0';
Run Code Online (Sandbox Code Playgroud)
或者操作是否在char array[4] = {0}任何赋值之前终止?