1

I have written two function in which I am passing vector of string to a particular function (PrintStringVector) just to print content and in second function, passing the array of pointers to print the content.The first function is working fine but second one is giving error which is below the my code.

#include <cmath>
#include <stdlib.h>
#include <cstdio>
#include <string>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int n;

void PrintStringVector(vector<string> & v){


    for(int i=0;i<v.size();i++){ cout<<v[i]<<endl;}

}


void PrintStringArray(const char *arr[n]){

    for(int i=0;i<n;i++){ cout<<arr[i]<<endl;}



}

int main() {

    vector<string>  vec;                    
    cin>>n;
    const char *arr[n];

    for(int i=0;i<n;i++){
        string str;
        cin>>str; 
        vec.push_back(str);
        arr[i]=str.c_str();
    }

    PrintStringVector(vec);
    PrintStringArray(arr);


    return 0;
}

ERRORS:

        vinod@vinod-Inspiron-3537:~/Documents/hackerrank$ g++       passing_vector_of_string_or_passing_2d_array.cpp 
        passing_vector_of_string_or_passing_2d_array.cpp:17:35: error: expected ‘,’ or ‘...’ before ‘arr’
        void PrintStringArray(const char* arr[n]){
                                           ^
        passing_vector_of_string_or_passing_2d_array.cpp: In function ‘void PrintStringArray(const char*)’:
        passing_vector_of_string_or_passing_2d_array.cpp:19:33: error: ‘arr’ was not declared in this scope
        for(int i=0;i<n;i++){ cout<<arr[i]<<endl;}
                                         ^
        passing_vector_of_string_or_passing_2d_array.cpp: In function ‘int main()’:
        passing_vector_of_string_or_passing_2d_array.cpp:40:25: error: cannot convert ‘const char**’ to ‘const char*’ for argument ‘1’ to ‘void PrintStringArray(const char*)’
        PrintStringArray(arr);
3
  • Another problem with your code is that you are storing the address of memory managed by a local variable that goes out of scope str when you do arr[i] = str.c_str(); This will lead to undefined behavior. Commented Nov 1, 2015 at 15:28
  • Since I had not better solution to assign string str to character pointer.I think c_str() convert c++ string to c string (made of char array). Commented Nov 1, 2015 at 16:06
  • c_str() returns back a pointer that is C compatible, but this pointer is only valid while the std::string object that returned it exists. You are using it after this point. Commented Nov 2, 2015 at 15:04

2 Answers 2

2
const char *arr[n]

This is not a valid declaration (unless n is a constant expression). C++ has no variable length arrays.

"But it works for me inside main!'

That's because g++ implements an extension to C++. This extension does not seem compatible with C variable-length arrays, and is generally buggy. Don't use it.

"But how can I have comparable functionality?"

Use std::vector. Do not use pointers, new[] and delete[] unless you know extremely well why you need these low-level primitives.

Sign up to request clarification or add additional context in comments.

Comments

2

In C++ you cannot have VLA (variable length arrays). Since the size of the array arr is only known at runtime you cannot use it as a size of a fixed size array. You should use new to allocate it. E.g.

const char **arr = new const char*[n];

Also modify the function signature like this

void PrintStringArray(const char *arr[]){

or like this

void PrintStringArray(const char **arr){

Finally remember to delete arr once you are finished with it.

delete[] arr;

1 Comment

No. You shouldn't use new to allocate arrays. (You can, but should not). You should use std::vector.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.