小编Unh*_*eep的帖子

C++ 我应该使用什么类型的指针来存储`algorithm` `remove` 函数的输出地址?

我试图将algorithm removeC++ 中函数返回的地址存储在一个变量中,但未能找到。我试过int*char*。两者都抛出了错误。

使用 Visual Studio CL,错误是: error C2440: '=': cannot convert from '_FwdIt' to 'int *'

使用MinGW,错误是: cannot convert '__gnu_cxx::__normal_iterator<char*, std::__cxx11::basic_string<char> >' to 'int*' in assignment

我应该如何存储这样的地址?

我正在尝试的代码:

#include <stdio.h>
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

int main (void) {
  string line ("This is an example sentence.");
  int* newEOL;
  newEOL = remove(line.begin(), line.end(), ' ');
  printf("%p\n", newEOL);
}
Run Code Online (Sandbox Code Playgroud)

c++ variables pointers

2
推荐指数
1
解决办法
43
查看次数

为什么同一个指针的地址不同呢?

我正在尝试 C++ 上的链接列表,当我尝试打印列表的数据时,它给出了有趣的结果,告诉我列表为空。当我在 main 中打印变量的地址和在函数中打印同一变量的参数地址时,会给出不同的结果。有谁可以帮我解释一下吗,谢谢!这是代码:

#include <iostream>
using namespace std;

typedef struct link_node{
    int data;
    link_node* next;
};

void iter(link_node* head){
    link_node* cur = head;
    cout<<"The address of head in function: "<<&head<<endl;
    while(cur!=NULL){
        cur = cur->next;
        cout<<cur->data<<' ';        
    }
}

link_node *create(int a){
    link_node* head;
    link_node* cur;
    head =(link_node*) malloc(sizeof(link_node));
    cur = head;
    for(int i=a;i<a+10;i+=2){
        link_node * node = (link_node*) malloc(sizeof(link_node));
        node->data = i;
        cur->next = node;
        cur = node;
    }
    cur->next = NULL;
    return head;
}

int main(){
    link_node* …
Run Code Online (Sandbox Code Playgroud)

c++ pointers c++11

0
推荐指数
1
解决办法
132
查看次数

标签 统计

c++ ×2

pointers ×2

c++11 ×1

variables ×1