Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone.. Just one question if anyone can help me..

How to input the size of an array with the user input?


C++
#include <stdio.h>
#define LINE 20

int main()
{
    char *select[LINE]; // size will be 20
}


.. but if I have this:

C++
#include <stdio.h>

int main()
{
    unsigned int nr = 0;

    printf("Input a number: ");
    scanf("%u", &nr);

    char *select[nr]; // Can this be done ?
    ...
    ..
    .
}


What I have tried:

I didn't try anything yet since I just begun programming.
Can anyone explain how to do this ?
Posted
Updated 21-Mar-21 22:43pm

Quote:
char *select[nr]; // Can this be done ?

Yes, that is possible since C99 (so every modern compiler should allow it).
The following program
C
#include <stdio.h>
  
int main()
{
    unsigned int nr = 0;

    printf("Input a number: ");
    scanf("%u", &nr);

    char *select[nr];

    if ( nr > 1 )
    {
      select[0] = "Hi";
      select[1] = "there";
      printf("%s %s\n", select[0], select[1]);
    }

    return 0;
}


compiles and runs fine (I have used GCC 9).
 
Share this answer
 
Comments
Maciej Los 22-Mar-21 4:09am    
5ed!
CPallini 22-Mar-21 8:04am    
Thank you!
OriginalGriff 22-Mar-21 4:15am    
5!
CPallini 22-Mar-21 8:04am    
Thank you!
CPallini is right, but a better approach would be to use malloc to allocate the array, since that will place it on the heap instead of the stack.

Arrays can get very large, very quickly - and stack space is limited, usually about than 1MB where the heap can be "as much as your system will allow".
C++
char** select = (char**) malloc (nr * sizeof(char*));
You can then use it as an array of strings without any problems. Remember to free the memory when you are finished with it!

This also has the advantage of persistence: if the array is declared directly within a function, then the stack space is freed up when the function returns, so the string pointers array cannot be used or returned from the function.
 
Share this answer
 
Comments
CPallini 22-Mar-21 8:05am    
Have my 5 as well.
Okay now I understand.. I have one more question if I may ask. If I read lines from a text.txt file like this:

C++
#include <stdio.h>

int main()
{
    FILE *file = fopen("text.txt", "r");
    char buffer[256] = {};
    unsigned int nr = 0;

    while(fgets(buffer, sizeof buffer, file) != NULL)
    {
        nr++;
    }

    char *select[nr]; // is this valid as well ?
    ...
    ..
    .
    fclose(file);
}


Can this be done as well ?
 
Share this answer
 
Comments
Richard MacCutchan 22-Mar-21 5:03am    
Yes it works; but by the time you create your pointer array you have lost all the text from your file except the last line. That may not be what you are aiming for.
M@gelearn 22-Mar-21 5:32am    
Okay.. I understand, thank you for your answer..
M@gelearn 22-Mar-21 5:34am    
I will post in short time what I do really want to do with this array of pointers, and I hope someone will make me understand why I can't assign the size like I do..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900