简体   繁体   中英

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

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[]? Why is the compiler telling me that I am trying to set the value of the pointer to the value of i[]? I want to set the value of the pointer to the ADDRESS of i[]. If I add an & I get the same error.

Error 2 obviously follows error 1. It is the same error, just backwards.

My question is, then, what the heck am I doing wrong when I am trying to point *ptr in func() at the array 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.

int *ptr; declares a pointer to int , so ptr is a pointer: a variable that stores the memory location of some 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.

When you do *ptr = i; you are attempting to set the int pointed to by ptr equal to i , which is an array of 8 int s. 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.

This should just be ptr = i; , but even that isn't necessary, since you could just return i; and i would decay into a pointer to the first element of the array.

Similarly, *test = func(bits); attempts to set the int pointed to by test equal to the pointer returned by func . It should just be test = func(bits); , in which case the address that test contains will become the one that func returned. In this case test will point to the first element of i .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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