为什么以下作业不起作用?如果可能的话,我想要一个低级的解释。另外,这是我得到的编译器错误:'char*' 到 'char [20]' 赋值中的类型不兼容
class UCSDStudent {
char name[20];
public:
UCSDStudent( char name[] ) {
//this-> name = name; does not work! Please explain why not
strcopy( this -> copy, copy ); //works
}
};
Run Code Online (Sandbox Code Playgroud) 我正在尝试学习C中的指针,并且已经完成了概念.我遇到了这个实验室问题,并尝试为它编写解决方案.
/* p1.c
Write a short C program that declares and initializes (to any value you like) a
double, an int, and a char. Next declare and initialize a pointer to each of
the three variables. Your program should then print the address of, and value
stored in, and the memory size (in bytes) of each of the six variables.
Use the “0x%x” formatting specifier to print addresses in hexadecimal. You
should see addresses that look something like this: "0xbfe55918". …Run Code Online (Sandbox Code Playgroud) 我一直在使用 stringWithUTF8String 将我的 NSData 转换为 NSString,如下所示:
if ([[NSString stringWithUTF8String:[responsedata bytes]] isEqualToString:@"SUCCESS"]){
dostuff...
}
Run Code Online (Sandbox Code Playgroud)
它运行良好;但是,自 8.2 iOS 更新以来,[[NSString stringWithUTF8String:[responsedata bytes]]返回 nil。
我通过使用以下代码解决了这个问题:
NSString *responseDataString = [[NSString alloc] initWithData:responsedata encoding:NSUTF8StringEncoding];
if ([responseDataString isEqualToString:@"SUCCESS"]){
dostuff...
}
Run Code Online (Sandbox Code Playgroud)
在这两种情况下responsedata,打印的描述是相同的:<OS_dispatch_data: data[0x7aeb6500] = { leaf, size = 7, buf = 0x7c390360 }>
我的问题是:为什么第一个选项会返回 nil,为什么会在 iOS 8.2 更新后突然返回?
这是一个关于这个问题的问题: (char*)0在c中是什么意思?
答案稍微偏离了解释究竟是什么答案,但最后的答案提到它是一个指向地址0处的字符的指针,它是空的.这给我带来了两个疑问.
在c中,我们可以给char*9并说它是指向地址9的指针吗?我们不会收到任何错误或警告吗?
好吧,假设(char*)0确实是指向地址0处字符的指针,但这个地址0是什么意思?我的意思是我们怎么能说它是空的呢?在那种情况下,(char*)1或(char*)2等的值是多少?
编辑:只是把它放在这里是否有帮助.当我发现execllinux系统调用中的最后一个参数为null并且我看到了一个相当奇怪的语法时,我初步搜索了这个问题:(char*)0.
谢谢.
以下代码行会在 HP-UX 的 C++ 编译器中生成编译器警告:
strcpy(var, "string")
Run Code Online (Sandbox Code Playgroud)
输出:
error #2167: argument of type "unsigned char *"
is incompatible with parameter of type "char *"
Run Code Online (Sandbox Code Playgroud)
请注意:var是unsigned char *这里 - 它的数据类型超出了我的控制范围。
两个问题:
strcpy?我想在struct中初始化一个char*字符串.
这是结构:
typedef struct __String {
char* data; // C-string data,
unsigned* copyRef; // number of copy reference, delete only when the copyRef is 0
bool isACopy; // this boolean is true when *data is a ref to an other MyString
__String () {
data = 0;
isACopy = false;
copyRef = new unsigned();
*copyRef = 0;
return;
}
void addCopyRef() {
*copyRef++;
}
void removeCopyRef() {
*copyRef--;
}
} *MyString;
Run Code Online (Sandbox Code Playgroud)
这就是它崩溃的地步..
void Initialize(MyString& string){
string->data = new char[LENGHT];
string->data[0] …Run Code Online (Sandbox Code Playgroud) 我真的很困惑在字符串上使用指针.感觉他们遵守不同的规则.请考虑以下代码
char *ptr = "apple";// perfectly valid here not when declaring afterwards like next
ptr = "apple"; // shouldn't it be *ptr = "apple"
Run Code Online (Sandbox Code Playgroud)也printf()表现不同-
printf("%s", ptr) // Why should I send the address instead of the value
Run Code Online (Sandbox Code Playgroud)我还在一本书中看到了以下代码
char str[]="Quest";
char *p="Quest";
str++; // error, constant pointer can't change
*str='Z'; // works, because pointer is not constant
p++; // works, because pointer is not constant
*p = 'M'; // error, because string is constant
Run Code Online (Sandbox Code Playgroud)我无法理解应该暗示什么
请帮忙,我在其他地方找不到任何信息
为什么声明const int8_t* cstr = "asdf";会出错
invalid conversion from ‘const char*’ to ‘const int8_t*’
是不是int8_t*和char*一样吗?我在这里错过了一些微妙的东西吗?!
码:
#include <stdio.h>
int main() {
char *str;
char i = 'a';
str = &i;
str = "Hello";
printf("%s, %c, %x, %x", str, i, str, &i);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我得到这个输出:
Hello, a, 403064, 28ff0b
Run Code Online (Sandbox Code Playgroud)
我有两个疑问:
如何在不为其分配任何内存的情况下存储字符串.str是一个字符指针,指向char变量的位置i.当我添加str = "Hello";不是我使用5那个位置4没有分配的字节?
因为,我的代码str = &i;不应该str与&i具有相同的值时,我的printf呢?当我删除str = "Hello";声明str并且&i是相同的.而如果str和&i是相同的话,我相信,当我说str = "Hello"这应该覆盖'a'与'H'其余'ello\0'进入后续字节.
我相信整个问题都是 …
假设我有一个使用以下方式跟踪类型的结构const char*:
struct Foo {
const char* type;
}
Run Code Online (Sandbox Code Playgroud)
假设我在整个程序中仅使用字符串文字分配此值:
Foo bar;
bar.type = "TypeA";
Foo baz;
baz.type = "TypeB";
Run Code Online (Sandbox Code Playgroud)
==使用常规值而不是 来比较该值是否安全strcmp?
if (bar.type == baz.type) {
printf("Same\n");
} else {
printf("Different\n");
}
Run Code Online (Sandbox Code Playgroud)
出于性能原因我想这样做。