0

I dont really understand pointers and how to call and create dynamic arrays despite spending the time to educate myself on them and I am running into an error on the following line:

char *dArray = alphabet(N);

Here on my instructions and what Im trying to do:

  1. The program first asks user to enter the number of elements for an array. Let’s call this number N.
  2. It then creates a dynamic array of size N+ 1, containing Nrandom lowercase alphabet letters between ‘a’ and ‘z’. Make sure the last element is a null character ‘\0’. – Here, make sure to use dynamic memory allocation (using new command) to allocate memory space for the array, which is exactly why we call it a dynamic array.
  3. After creating the array, the program displays the entire array.
#include <stdio.h>
#include <iostream>
#include <algorithm> // for std::find
#include <iterator> // for std::begin, std::end
#include <ctime>
using namespace std;

char alphabet(int N)
{
    char *dArray;
    dArray= new char[N+1];
    dArray[N]= '\0';
    int i;
    srand(time(NULL));
    for (i=0; i<N; i++)
    {
        int r = rand()%26;
        char letter='a'+r;
        cout<<"dArray["<<i<<"] is: "<< letter<<endl;
        dArray[i]= letter;
    }
    
    return *dArray;
}

int main()
{
    int arrayN;
    int N;
    printf("enter the number of elements: ");
    cin >> N; 
    char *dArray = alphabet(N);
    for (int i=0; i<=N; i++)
    {
        cout<<"dArray["<<i<<"] "<<dArray[i]<<endl;
    }
}
6
  • 10
    Don't use raw owning pointer, use std::string or std::vector instead. Commented Jun 21, 2022 at 16:49
  • 4
    The return type of alphabet now is char, not char* Commented Jun 21, 2022 at 16:52
  • 3
    I dont really understand pointers and how to call and create dynamic arrays Good, do what @Jarod42 recommends and save yourself a lot of heartache. Commented Jun 21, 2022 at 16:53
  • 1
    In English, return *dArray; means return the first character in the array dArray. The pointer to the array is lost and without that pointer it's next to impossible to free the array. This is called a memory leak. Commented Jun 21, 2022 at 16:53
  • Future bug: Any time you see a <= in the exit condition of a loop iterating a container, odds are really good you are looking at a bug. Check the range of i in for (int i=0; i<=N; i++) to make certain you aren't going one past the end. Commented Jun 21, 2022 at 16:57

2 Answers 2

1

you have declared the return type wrong. it should be char *

char *alphabet(int N) // <<<<====
{
    char *dArray;
    dArray= new char[N+1];
    dArray[N]= '\0';
    int i;
    srand(time(NULL));
    for (i=0; i<N; i++)
    {
        int r = rand()%26;
        char letter='a'+r;
        cout<<"dArray["<<i<<"] is: "<< letter<<endl;
        dArray[i]= letter;
    }
    
    return dArray; // <<<<<<=====
}

but much better would be std::string or std::vector<char>

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

1 Comment

@DandyApe dont forget to release the dynamic array. delete [] dArray; inside the main() function.
0

You should avoid raw owning pointers, and prefer std::string.

Using std, you might do something like:

std::string random_letters(std::size_t N, std::mt19937& rnd)
{
    std::string res(N, '\0');

    const char alphabet[] = "abcdefghijklmnopqrstuvwxyz";
    std::uniform_int_distribution<int> dist(0, 25);

    std::generate(res.begin(), res.end(), [&](){ return alphabet[dist(rnd)]; });

    return res;
}

Demo

Comments

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.