最近我正在学习C++中的Link-List.这是我的代码:
#include <iostream>
using namespace std;
class Node{
Node* next;
int num;
public:
Node(int num){
this->num = num;
}
void connect(Node* next){
this->next = next;
}
Node* next_node(){
return next;
}
void COUT(){
cout<<this->num<<endl;
}
};
void swap(Node* n1,Node* n2){
static Node* n = n1;
n1 = n2;
n2 = n;
}
class List{
Node* head;
Node* last;
public:
List(){
head = 0;
last = 0;
}
void insert(Node* n){
if(head==0){
head = n;
last = n;
}
else{
last->connect(n); …Run Code Online (Sandbox Code Playgroud) 假设我有一个指向整数的指针数组(即每个元素都是指向int的指针)
int** ptrArray;
Run Code Online (Sandbox Code Playgroud)
我想阻止更改数组条目指向的整数
我需要把const放在哪里?
1. const int ** ptrArray
2. int const ** ptrArray
3. int *const* ptrArray
4. int ** const ptrArray
Run Code Online (Sandbox Code Playgroud)
对此有什么规定吗?像"第一个const保护数据","第二个const保护指针"等等?
const的位置背后是否有任何逻辑?放置的地方与保护的地方之间有什么联系?
这对我来说是一个令人困惑的问题,如果有人可以给我任何指导或链接,我会真的很高兴我可以阅读更多关于如何以及在何处使用const基于我想要保护的内容(如果我需要使用三维数组中的const或左右)
我有一些C代码打印字符串char数组两次.
Code:
char* twice(char *s) {
int size=strlen(s),i=0;
int length=size*2;
char check = s[size-1];
char* s2 = malloc(length * sizeof(char));
char* reset = malloc(size * sizeof(char));
memcpy(reset, s, size * sizeof(char));
while (i<length) {
printf("%s\n", s);
s2[i] = *s;
if(s2[i] == check && i == size-1){
s = reset;
}else s++;
i++;
}
return s2;
}
int main(){
char s[] = "hello1234";
printf("%s\n", twice(s));
return 0;
}
Output:
hello1234
ello1234
llo1234
lo1234
o1234
1234
234
34
4
hello1234
ello1234
llo1234 …Run Code Online (Sandbox Code Playgroud) #include<stdio.h>
int main(void)
{
int arr[3] = {2, 3, 4};
char *p;
p = (char*)arr;
printf("%d", *(int*)(p+1));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我在指针上做这个问题.我期望输出是一些垃圾值,因为从char*转换为int*但输出总是50331648,这很奇怪.请解释
编辑:我在某些网站上看到了这个输出问题,所以需要根据给定的指令输出
我从讲师那里听说,sizeof(某些指针)总是在机器上相等.例如,如果它是64位,则所有指针都是8字节,在32位4字节中.所以,sizeof当我想指定数组的大小时,我喜欢使用
int arr[] = {2, 5, -1, 10, 9};
int size = sizeof(arr) / sizeof(int)
Run Code Online (Sandbox Code Playgroud)
我假定上面的代码的int需要4,所以20/4给出了元件的数目arr.
int arr[] = {2, 5, -1, 10, 9};
int size = sizeof(arr) / sizeof(int*)
Run Code Online (Sandbox Code Playgroud)
我也假设sizeof(some pointer)take 4,所以20/4再次给出了元素的数量arr.
当我将它应用于double数组时,我很困惑.我认为它应该不起作用.但是,它有效.
double arr[] = {2, 5, -1, 10, 9};
int size = sizeof(arr) / sizeof(double*);
Run Code Online (Sandbox Code Playgroud)
如果每个指针在一台机器中占用相同的大小,结果怎么样呢?问题可以解释一下吗?编辑
我把*arr和双指针混合在一起
int compare (const void * a, const void * b)
{
return ( *(int*)a - *(int*)b );
}
Run Code Online (Sandbox Code Playgroud)
我不明白表达*(int*)a?
假设我有一个带指针输入的函数
void f(int *a, int *b) {
if (*a < *b) {
printf("hello!\n");
}
}
Run Code Online (Sandbox Code Playgroud)
哪里*a < *b是正确的行为.
在gcc中是否有警告我可以打开,所以每当我编写代码时
a < b
Run Code Online (Sandbox Code Playgroud)
时a,b被int *编译器会警告我?
我在buf0buf.cc这里读了mysql的innodb缓冲区源代码的代码:
我得到了这个:
&buf_pool->watch[0]
声明的价值是什么?地址?或其他价值?
代码是什么意思?(语法含义)
int main
{
void* i;
printf("%d",i);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出:(垃圾值)void指针如何确定没有类型转换的数据类型?
我正在尝试编写一个简单的程序,它将使用指针反转用户输入.这是我第一次使用指针,理论上我的程序似乎可以工作:有一个数组,将用户输入写入数组,指向头部的一个指针,另一个指向结尾,并使while循环执行休息.但是,我的程序运行不正常.我的问题是,我究竟做错了什么?
继承我的代码:
#include <iostream>
#include <string>
using namespace std;
int main() {
char user_input[1000] = " ";
cout << "Enter a word to be reversed: " << endl;
cin >> user_input;
int myChar = sizeof(user_input) - 1;
char *start = user_input;
char *end = user_input + myChar - 1;
while (start < end) {
char save = *start;
*start = *end;
*end = save;
start++;
end--;
}
cout << user_input;
}
And my output:
Enter a word to be reversed: …Run Code Online (Sandbox Code Playgroud)