473,388 Members | 1,256 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

string array

Hi,

I would like to use array of array to store different string, or if you have
better idea to store different string in array index.
How can I achieve that ? Any example will be greatly appreciate.

Thanks.
Nov 14 '05 #1
8 1397
Hi.

This really depends on what you are going to use this for, but an array
of strings is the way I usaly do it. If you could describe what you are
going to use this index of strings for, an example should be possible
to make. Is it something you are going to declare once, or is it going
to grow during runtime?

Nov 14 '05 #2
an array pointer to hold mutiple string.
Not declare once, but the array index is fixed with empty contents, and the
contents will be updated during runtime using strcpy.

"bjrnove" <bj*****@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Hi.

This really depends on what you are going to use this for, but an array
of strings is the way I usaly do it. If you could describe what you are
going to use this index of strings for, an example should be possible
to make. Is it something you are going to declare once, or is it going
to grow during runtime?

Nov 14 '05 #3
So, something like this will help?

You wil of course have to free the memory of each string at the end, or
when you are done.

#include <stdio.h>
#include <string.h>

/* This function returns the number of items in the array of strings.
*/
size_t StringsSize(char** ppszStrings)
{
char** ppszTmp = ppszStrings;

if(!ppszStrings)
return 0;

while(*ppszTmp)
ppszTmp++;

return (ppszTmp - ppszStrings);
}
/* This function adds a string to the end of the array of strings */
int AddString(char*** pppszStrings, char* pszString)
{
char** ppszStrings = *pppszStrings;
size_t nSize = 0;

nSize = StringsSize(ppszStrings);

if(!(ppszStrings = (char**)realloc(ppszStrings, sizeof(char**) *
(nSize + 2)))){
return errno;
}

if(!(ppszStrings[nSize] = (char*)malloc(strlen(pszString) + 1))){
return errno;
}

strcpy(ppszStrings[nSize], pszString);

ppszStrings[nSize + 1] = NULL;

*pppszStrings = ppszStrings;
return 0;
}

int main()
{
char **ppszStrings = NULL;
int i,j;

AddString(&ppszStrings, "Test1");
AddString(&ppszStrings, "Test2");
AddString(&ppszStrings, "Test3");

for(i = 0, j = StringsSize(ppszStrings); i < j; i++){
printf("String %i: %s\n", i + 1, ppszStrings[i]);
}

return 0;
}

Nov 14 '05 #4
And you could use something like this to delete one string somewhere in
the array.

int RemoveString(char*** pppszStrings, int nIndex)
{
char** ppszStrings = *pppszStrings;
int i, j;
size_t nSize = 0;

nSize = StringsSize(ppszStrings);

/* Check if the index exist in the array. */
if(nIndex >= nSize)
return 1; /* Should be defined as an error constant */

free(ppszStrings[nIndex]);

for(i = nIndex + 1, j = nSize + 1; i < j; i++){
ppszStrings[i - 1] = ppszStrings[i];
}

if(!(ppszStrings = (char**)realloc(ppszStrings, nSize))){
return errno; /* FATAL ERROR. THIS SHOULD NOT HAPPEND,
* AND MOST PROBABLY WIL NOT */
}

*pppszStrings = ppszStrings;

return 0;
}

And something like this to delete it all.

int FreeStrings(char** ppszStrings)
{
int i,j;

for(i = 0, j = StringsSize(ppszStrings); i < j; i++)
free(ppszStrings[i]);

free(ppszStrings);
}

Nov 14 '05 #5


bjrnove wrote:
So, something like this will help?
The code appears good. However, I believe you have an almost
unnoticeable flaw in function AddString. Look over carefully
what will happen should function realloc fail or function
malloc fail(on the first call).
You wil of course have to free the memory of each string at the end, or
when you are done.
size_t StringsSize(char** ppszStrings)
{
char** ppszTmp = ppszStrings;

if(!ppszStrings)
return 0;

while(*ppszTmp)
ppszTmp++;

return (ppszTmp - ppszStrings);
}
/* This function adds a string to the end of the array of strings */
int AddString(char*** pppszStrings, char* pszString)
{
char** ppszStrings = *pppszStrings;
size_t nSize = 0;

nSize = StringsSize(ppszStrings);

if(!(ppszStrings = (char**)realloc(ppszStrings, sizeof(char**) *
(nSize + 2)))){
return errno;
}

if(!(ppszStrings[nSize] = (char*)malloc(strlen(pszString) + 1))){
return errno;
}

strcpy(ppszStrings[nSize], pszString);

ppszStrings[nSize + 1] = NULL;

*pppszStrings = ppszStrings;
return 0;
}


--
Al Bowers
Tampa, Fl USA
mailto: xa******@myrapidsys.com (remove the x to send email)
http://www.geocities.com/abowers822/

Nov 14 '05 #6
This was something I put together just to make an example. Yes it would
be a problem if malloc failed the first time. And it would definitly
create a memory leak. If realloc fails howewer, I don't see a problem.

Nov 14 '05 #7
This should fix the problem mentioned above.

int AddString(char*** pppszStrings, char* pszString)
{
char** ppszStrings = *pppszStrings;
size_t nSize = 0;

nSize = StringsSize(ppszStrings);

if(!(ppszStrings = (char**)realloc(ppszStrings, sizeof(char**) *
(nSize + 2)))){
/* If this fail I will return without making any changes to the

* string assay. If it fail the first time everything is NULL
* anyway, so that shouldn't be a problem. */
return errno;
}

if(!(ppszStrings[nSize] = (char*)malloc(strlen(pszString) + 1))){
int nErr = errno;
/* If this fails it's a bit of a problem. My solution is to
* realloc the array back to it's original size. */

if(!(ppszStrings = (char**)realloc(ppszStrings, sizeof(char**)
* (nSize + 1)))){
/* This shouldn't be possible since you're asking for less
memory than you
* already have. */

return 123; /* Should be defined as an FATAL ERROR constant
of some
* kind. If you get an error here the system is
unstable,
* and your program should quit. */
}
*pppszStrings = ppszStrings;
return nErr;
}

strcpy(ppszStrings[nSize], pszString);

ppszStrings[nSize + 1] = NULL;

*pppszStrings = ppszStrings;
return 0;
}

Nov 14 '05 #8


bjrnove wrote:
This should fix the problem mentioned above.

int AddString(char*** pppszStrings, char* pszString)
{
char** ppszStrings = *pppszStrings;
size_t nSize = 0;

nSize = StringsSize(ppszStrings);

if(!(ppszStrings = (char**)realloc(ppszStrings, sizeof(char**) *
(nSize + 2)))){
/* If this fail I will return without making any changes to the

* string assay. If it fail the first time everything is NULL
* anyway, so that shouldn't be a problem. */
return errno;
}

if(!(ppszStrings[nSize] = (char*)malloc(strlen(pszString) + 1))){
int nErr = errno;
/* If this fails it's a bit of a problem. My solution is to
* realloc the array back to it's original size. */

if(!(ppszStrings = (char**)realloc(ppszStrings, sizeof(char**)
* (nSize + 1)))){
/* This shouldn't be possible since you're asking for less
memory than you
* already have. */

return 123; /* Should be defined as an FATAL ERROR constant
of some
* kind. If you get an error here the system is
unstable,
* and your program should quit. */
}
No, you cannot simply realloc and return an error code. Either one
of the two realloc statements may move the array of pointers to a
new location. Therefore, the value of the char ** argument in function
main may no longer point to the array. See, below the function
modified that should correct the problem.

*pppszStrings = ppszStrings;
return nErr;
}

strcpy(ppszStrings[nSize], pszString);

ppszStrings[nSize + 1] = NULL;

*pppszStrings = ppszStrings;
return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

size_t ArrayCount(char **p);
int AddString(char ***p, const char *s);
void PrintArray(char **p);
void FreeArray(char ***p);

int main(void)
{
char **name = NULL;

AddString(&name,"George Washington");
AddString(&name, "John Adams");
AddString(&name, "Bill Clinton");
AddString(&name,"George W. Bush");
AddString(&name, "Abraham Lincoln");
puts("\tContents of the array of strings");
PrintArray(name);
FreeArray(&name);
return 0;
}

size_t ArrayCount(char **p)
{
size_t tmp;

if(!p) return 0;
for(tmp = 0;(*p); p++,tmp++) ;
return tmp;
}

int AddString(char ***p, const char *s)
{
char **tmp, *s1;
size_t ele_nr = ArrayCount(*p);

if(!p || !s) return 0;
if((tmp = realloc(*p,(ele_nr+2)*sizeof *tmp)) == NULL)
return 0;
tmp[ele_nr] = tmp[ele_nr+1] = NULL;
*p = tmp;
if((s1 = malloc(strlen(s)+1)) == NULL)
return 0;
strcpy(s1,s);
tmp[ele_nr] = s1;
return 1;
}

void PrintArray(char **p)
{
for( ; *p; p++)
printf("%s\n",*p);
return;
}

void FreeArray(char ***p)
{
char **tmp;

for(tmp = *p ; *tmp; tmp++) free(*tmp);
free(*p);
*p = NULL;
return;
}

--
Al Bowers
Tampa, Fl USA
mailto: xa******@myrapidsys.com (remove the x to send email)
http://www.geocities.com/abowers822/

Nov 14 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Don Starr | last post by:
When applied to a string literal, is the sizeof operator supposed to return the size of the string (including nul), or the size of a pointer? For example, assuming a char is 1 byte and a char *...
7
by: al | last post by:
char s = "This string literal"; or char *s= "This string literal"; Both define a string literal. Both suppose to be read-only and not to be modified according to Standard. And both have...
4
by: songkv | last post by:
Hi, I am trying to reassign an array of char to a string literal by calling a function. In the function I use pointer-to-pointer since I want to reassign the "string array pointer" to the string...
22
by: spike | last post by:
How do i reset a string? I just want to empty it som that it does not contain any characters Say it contains "hello world" at the time... I want it to contain "". Nothing that is.. Thanx
4
by: Simon Schaap | last post by:
Hello, I have encountered a strange problem and I hope you can help me to understand it. What I want to do is to pass an array of chars to a function that will split it up (on every location where...
14
by: Bob | last post by:
I have a function that takes in a list of IDs (hundreds) as input parameter and needs to pass the data to another step as a comma delimited string. The source can easily create this list of IDs in...
8
by: Jeff Johnson | last post by:
Hi, I've begun converting an ASP site over to .NET and I'm a novice at both the new platform as well as C#. I have a COM+ object that returns a string array when it is called. The size of...
17
by: Chad Myers | last post by:
I've been perf testing an application of mine and I've noticed that there are a lot (and I mean A LOT -- megabytes and megabytes of 'em) System.String instances being created. I've done some...
11
by: Zordiac | last post by:
How do I dynamically populate a string array? I hope there is something obvious that I'm missing here Option Strict On dim s() as string dim sTmp as string = "test" dim i as integer ...
14
by: Shhnwz.a | last post by:
Hi, I am in confusion regarding jargons. When it is technically correct to say.. String or Character Array.in c. just give me your perspectives in this issue. Thanx in Advance.
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.