简体   繁体   English

返回指向C ++中的整数数组的指针(从字符串转换)-“无效转换”错误

[英]Returning a pointer to an array of ints in c++ (converted from string) - “invalid conversion” error

Header: 标头:

#ifndef HEADER_H_INCLUDED
#define HEADER_H_INCLUDED

int i[8];

#endif

Main: 主要:

#include <iostream>
#include <string>
#include "header.h"

using namespace std

int main(){

int *test;
string bits = "10011011";

*test = func(bits);     //        ERROR    2   HERE
                        //my goal here is to have a pointer in main that
                        //points to the 1 address of the global var array i
}                       //if im totally missing the point and there is a 
                        //better way to do this please let me know

Function: 功能:

#include <iostream>
#include <string>

using namespace std

int *func(string str){

int l = str.size();
int *ptr;

for(int k=0; k < l; ++k){
    i[k] = s[k] - '0';
}
*ptr = i;             // ERROR    1    HERE
return ptr;           
}

Hi all, 大家好,

When I attempt to compile the above code, I get two errors as labeled. 当我尝试编译以上代码时,出现两个错误,如标签所示。 They are: 他们是:

Error 1: invalid conversiopn from int* to int Error 2: invalid conversion from int to *int 错误1:从int *到int的无效对话错误2:从int到* int的无效转换

It seems I must have a fundamental understanding of what I am working with here. 似乎我必须对在这里使用的内容有基本的了解。 Am I not setting *ptr to point at the first memory address of the array i[]? 我不是将* ptr设置为指向数组i []的第一个内存地址吗? Why is the compiler telling me that I am trying to set the value of the pointer to the value of i[]? 为什么编译器告诉我我正在尝试将指针的值设置为i []的值? I want to set the value of the pointer to the ADDRESS of i[]. 我想将指针的值设置为i []的ADDRESS。 If I add an & I get the same error. 如果我添加&,则会出现相同的错误。

Error 2 obviously follows error 1. It is the same error, just backwards. 错误2显然跟随错误1。这是相同的错误,只是向后。

My question is, then, what the heck am I doing wrong when I am trying to point *ptr in func() at the array i[]? 我的问题是,当我试图将func()中的* ptr指向数组i []时,我到底在做什么错? I DID try finding the answers elsewhere to no avail. 我想找到其他地方的答案都无济于事。

Thanks. 谢谢。

edit: this code is a rough transcription of the original (from another PC), so if you try to compile it and there are typos I apologize. 编辑:这段代码是原始代码(从另一台PC上)的粗略复制,因此,如果您尝试对其进行编译,并且有错别字,我深表歉意。

int *ptr; declares a pointer to int , so ptr is a pointer: a variable that stores the memory location of some int . 声明一个指向int的指针,所以ptr是一个指针:一个存储某个int的内存位置的变量。 Currently it doesn't point to anything in particular, just some random place in memory that you probably don't own. 当前,它并没有特别指向任何内容,只是您可能不拥有的内存中的随机位置。

*ptr dereferences ptr , so *ptr is the int that ptr points to. *ptr取消引用ptr ,因此*ptrptr指向的int

When you do *ptr = i; 当你做*ptr = i; you are attempting to set the int pointed to by ptr equal to i , which is an array of 8 int s. 您正在尝试将ptr指向的int设置为i ,它是8 int的数组。 In this context, the array decays into a pointer to its first element, so you're attempting to set an int equal to an int* , hence the error. 在这种情况下,数组会衰减为指向其第一个元素的指针,因此您尝试将int设置为等于int* ,从而导致错误。

This should just be ptr = i; 这应该只是ptr = i; , but even that isn't necessary, since you could just return i; ,但这不是必需的,因为您可以return i; and i would decay into a pointer to the first element of the array. 而且i会变成一个指向数组第一个元素的指针。

Similarly, *test = func(bits); 同样, *test = func(bits); attempts to set the int pointed to by test equal to the pointer returned by func . 尝试将test指向的int设置为等于func返回的指针。 It should just be test = func(bits); 它应该只是test = func(bits); , in which case the address that test contains will become the one that func returned. ,在这种情况下, test包含的地址将成为func返回的地址。 In this case test will point to the first element of i . 在这种情况下, test将指向i的第一个元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM