How Can You Determine the Number of Strings in a C++ Static Array?

In summary, the speaker is discussing how to use the "new" keyword to create a memory block for a static array of strings and how to determine the number of strings in the array. They also mention using a library function with the signature "char** doSomething(...)" and how to count the number of strings returned from that function. The speaker suggests using a while loop and incrementing a counter until the pointer is null. They also mention the possibility of using a parameter in the function to set the string count or return a pointer to the array. The speaker also mentions that since the array is static, there is no need to use the "new" keyword and provides an example of creating a static array of
  • #1
Medicol
223
54
1. Using new to create a memory block for a static array of strings. How to know the number of strings in an array of strings



Homework Equations


new


The Attempt at a Solution



1. static char ** array=new char*[]; // why the empty [] ? Will I get a minus point if coding like this ? My teacher says subscript data should be known in advance
.
2. If I have a library function that has the signature
PHP:
char** doSomething(...);

I am using it like this

PHP:
char**ret=doSomething();

I then would like to know the number of strings returned from that function.
I am taking the first course in IT programming, I chose C++ language.
 
Physics news on Phys.org
  • #2
I think you have to count them until the pointer is a null.

You could use a while loop and increment a counter while the ret pointer != NULL.
 
  • #3
Since the problem statement mentions "static" array of strings, then I assume it's a fixed number of strings, and that's it's an array of pointers to the first characters of a set of strings. The function could include a parameter that would be a pointer or a reference to a string count that the function sets. There could also be a parameter to return the pointer to the array, or it could be a return value. Note that a static variable's name scope is local, but it's duration is for the entire time a program (thread) is running, so it should be safe to return a pointer to a static variable in a function. Since these are static arrays, there's no need for new (I chose as to represent array of strings):

static char * as[4] = {"one", "two", "three", "four"};

optionally if using a null pointer to indicate the end of an array:

static char * as[5] = {"one", "two", "three", "four", (char *)0};

or an array with all null pointers:

static char * as[5];
 
Last edited:

Related to How Can You Determine the Number of Strings in a C++ Static Array?

1. What is the purpose of creating memory for an array?

The purpose of creating memory for an array is to allocate a specific amount of space in the computer's memory for storing data. This allows for efficient storage and retrieval of data, as well as the ability to manipulate the data in various ways.

2. How is memory allocated for an array?

Memory is allocated for an array by using a programming language's built-in functions or methods. These functions or methods will determine the size of the array and allocate the appropriate amount of memory accordingly.

3. Can the memory allocated for an array be changed?

Yes, the memory allocated for an array can be changed. This can be done by using functions or methods that allow for resizing of the array, or by creating a new array with a different size and transferring the data from the old array to the new one.

4. What happens if there is not enough memory available for an array?

If there is not enough memory available for an array, an error will occur. This can be handled by using exception handling techniques in programming, or by freeing up memory from other resources in the computer.

5. How does creating memory for an array affect the performance of a program?

The act of creating memory for an array itself does not significantly affect the performance of a program. However, the size of the array and how it is used can impact the overall performance. Using large arrays or inefficient methods of accessing and manipulating the data can slow down the program's execution.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
2
Replies
43
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
17
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
11
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
Back
Top