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

  • Thread starter Thread starter Medicol
  • Start date Start date
  • Tags Tags
    Array Memory
Click For Summary
SUMMARY

The discussion focuses on determining the number of strings in a C++ static array of strings. It highlights the use of a while loop to count strings until a null pointer is encountered. Participants emphasize the importance of defining the size of the array in advance, as well as the implications of using static variables. The conversation also suggests using a function parameter to return the string count or the pointer to the array.

PREREQUISITES
  • Understanding of C++ static arrays
  • Knowledge of pointer manipulation in C++
  • Familiarity with dynamic memory allocation using 'new'
  • Basic programming concepts such as loops and function parameters
NEXT STEPS
  • Research C++ pointer arithmetic and its applications
  • Learn about C++ static vs dynamic memory allocation
  • Explore the use of null pointers in C++ arrays
  • Study function parameters and return types in C++ for better data handling
USEFUL FOR

Beginner C++ programmers, computer science students, and anyone interested in understanding memory management and array handling in C++.

Medicol
Messages
226
Reaction score
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
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.
 
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:

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
Replies
43
Views
5K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 12 ·
Replies
12
Views
3K
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 3 ·
Replies
3
Views
4K