Array with unknown size

Suppose I declare an array. The C++ language demands I declare a size along with the name. I must state a[20] or b[5] etc etc. Suppose further that this array is to be used as a vacancy of user input. So we daclare an array:

char user_input[20]

So thats a character array to be filled in by the user. Suppose its the users name. Ultimately: HOW DO I MAKE THAT ARRAY TERMINATE UPON END OF NAME. In other words, if the user puts in: "John", then that array will have 4 elements (or will it have 5 including the '\0'?). This means we will have 15/16 elements still open waiting for the user to "fill them up". How does one get around this?
What are you talking about.
The array has 20 available spaces.
If you want to use it as a C-style string - you copy your text to the array
and ensure that it is terminated with a '\0' (operations/functions like gets() or cin >> user_input so the '\0' automatically).

In the case of "John" - 5 will be used - the other 15 are unused in this case.

there is no "still open waiting for the user to "fill them up" in the way you have described it.
Wouldn't it be best to use a string in this case?

Since in terms of memory wise it'll use 1 byte for every character and I believe one byte for the '\0'
Just take your input by using for, and when '\n' is detected, make the program delete last ( release) parameter.
I should have been more explicit in my question. Ill clarify it more by using the actual example that's bothering me. Suppose I want to search a word in a paragraph (example)....

So the paragraph (with new line characters) is loaded as an array of characters.
Now I prompt the user to search any word in that paragraph. This word is also an array called char search [x]. Note the 'x' that im using for now. thats the issue im having. The size of that array depends on what the user types. It could be "John" or "because", i.e. variable size character arrays.

Now you may immediately say, why not just use a size 100 array and the user fills in whatever number under 100 and you simply add a '\0' termination. But unfortunately it simply doesnt work.

the text file (an array) and search word (array) are declared:


char text[36000],search_word[11];

The search function called strindex is written:

1
2
3
4
5
6
7
8
9
10
11
int strindex(char s[], char t[])
{
int i, j, k;
for (i = 0; s[i] != '\0'; i++) {
for (j=i, k=0; t[k]!='\0' && s[j]==t[k]; j++, k++)
;
if (k > 0 && t[k] == '\0')
return i;
}
return -1;
}


Note that it returns the position in the array that the start of the search word character is in. Also note that it returns -1 upon error.

Now to prompt the user for an input (asking for the search word):

1
2
3
4
5
6
for(j=0;j<=10/*c!='\n'*/;j++)
{
c=getchar();
search_word[j]=c;//getchar

}


So what is the problem im having? The following:
If my search word is exactly 11 characters long (As declared in the first code I posted) then it will find that array position in the file. So thats good. However, that search word must be 11 characters long. I cant put any other search word other than 11 characters. You may reply saying why not try?:

1
2
3
4
5
6
7
for(j=0;j<=10/*c!='\n'*/;j++)
{
c=getchar();
search_word[j]=c;//getchar
if(search_word[j]=='\n')
search_word[j]=='\0';
}


Yes it does terminate it under 11 characters but when its passed to strindex (the search function) it returns an error (-1), in other words, it didnt find that search word.

I may know what the problem is but I dont have a solution.

Am I right in saying that because I terminated it with a '\0' character, that '\0' is itself part of the search word and thats why it cannot find the search word. Because in the file/paragraph/text, the only '\0' is at the very end of the array. How do I overcome this?
There is a standard C function strstr() that will search one string for another.

If you're using C++, you should use the standard string class for strings. That way you won't have to worry about memory management, string lengths and the like.
But say I wanted to make the whole word search myself "manually". I know its pointless, less efficient than other methods, but just for educational purposes I'm experimenting. So how does one get around the problem above using C and not using any library functions? Doesnt "malloc" come in somewhere usefull here?
You'd simply have to use a less naive approach. Before comparison, you check for the position of the '\0' in the string, and stop the comparison at that point.
Something like that:
1
2
3
4
5
6
7
8
9
10
if(!str[j]) //
{
     break;
}
else {
    if(str[j] == other_str[j]
    {
     // whatever
    }
}


Note: this doesn't do what you want to, it should just give you an idea.
You can create an array dynamically without knowing the size at compile time.

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>

int main()
{

int someValue;
std::cin >> someValue; // Now have a value based on user input that was not known at compile time
char* anArray = new char[someValue]; //anArray is now a char array of size someValue

delete anArray;

return 0;
}
Moschops wrote:
You can create an array dynamically without knowing the size at compile time.

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>

int main()
{

int someValue;
std::cin >> someValue; // Now have a value based on user input that was not known at compile time
char* anArray = new char[someValue]; //anArray is now a char array of size someValue

delete anArray;

return 0;
}



Yep you're right, I tried that approach and it works. Check out below (a little messy however):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <stdio.h>

int strindex(char s[],char t[])
{
int i,j,k;
for (i=0;s[i]!='\0';i++)
    {
for (j=i,k=0;t[k]!='\0'&&s[j]==t[k];j++,k++)
;
if (k>0&&t[k]=='\0')
return i;
    }
return -1;
}
//------------------------------------------------------------------------------
int main ()
{
    int c,i,j,n=-1,k;
    char a[100],text[140],ch='a';
    for(j=0;j<=99;j++)
    text[j]=ch++;
    printf("%s""\n""\nEnter search word for above text...\n",text);
    for(i=0;c!='\n';i++)
    {
    c=getchar();
    a[i]=c;
    ++n;                // the size of this array, before '\n', is being "counted"
    }
    a[i]='\0';
    printf("Search word is: ""%s""Size of search word is: ""%d",a,n);
    printf("\nEnter anything to continue: ");
    getchar();
    char b[n];
    for(k=0;k<n;k++)  // here the new array is being created of size n counted before
    b[k]=a[k];
    b[k]='\0';
    printf("%s""\n""%s",b,a);
    getchar();
    getchar();
    printf("%d",strindex(text,b));
    getchar();
    getchar();
}


Note that I made a string "a" of size 100. Once you enter a search word below 100 then "n" increments until the size of that string is known. Then this value n is used to make an array "b" of that size "n". Just one more question though... If you put 'J' 'O' 'H' 'N' '\0' in a character array of size 10, are the other 5 elements "still there" but unused? in other words only half that array is filled up but the other 5 elements are still somehow allocated and hanging around in memory. I dont know how to question it but I hope ya'll know what I mean.
Last edited on
Yes, they are still there. '\0' into a char is simply the number zero; there's nothing inherently special about it.
Topic archived. No new replies allowed.