Computing.Net > Forums > Programming > VC++ array of strings

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

VC++ array of strings

Reply to Message Icon

Name: concentriq
Date: November 11, 2002 at 18:45:25 Pacific
OS: WXP
CPU/Ram: 550/512
Comment:

Hi All. Again I do applolgize if it is too trivial, but I havent worked much with strings, and this one is pissing me off.

here is the problem:
As you can see in the attached code main calls function get_word()which returns a null terminated string (this part works OK).

the call is made 100 times, and each time the function is called, I want to put the value it returns into the array of strings (errz[j]). But for some reason it throws an access violation.

If someone knows the answer, id much appreciate it.

Thank you and here is a code for reference:


***********************************


#include
#include
#include
#include
#include
#include

using namespace std;

string get_word(int sz);

int main()
{
    char *errz[100];
    string tmp;
    for(int j=0;j1000)
        sz = 1000;

    end = sz;
    max = 1;
    ttt = rand()%100;
    for(int j=0;jend)
        sz = end;
    k = 0;
    for(int i=start;i    {
        rrr[k] = sss[i];
        k++;
    }
    rrr[sz] = '\0';
    return rrr;
}



Sponsored Link
Ads by Google

Response Number 1
Name: cup
Date: November 11, 2002 at 19:32:58 Pacific
Reply:

This site isn't very good for posting code. It has problems with the less-than and greater-than signs. Try www.codeguru.com or www.tek-tips.com


0

Response Number 2
Name: concentriq
Date: November 11, 2002 at 20:19:38 Pacific
Reply:

ooops. i didnt realize that most of the code disappeared. anyways here is a repost, but less than are [, and greater than are ]:

***********************************
#include[iostream]
#include[string]
#include[math.h]
#include[windows.h]
#include[time.h]
#include[ctype.h]

using namespace std;

string get_word(int sz);

int main()
{
char *errz[100];
string tmp;
for(int j=0;j[100;j++)
{
tmp = get_word(3);
errz[j] = tmp;
}
return 0;
}


string get_word(int sz)
{
int ttt;
char sss[1199];
char sss1[99];
char rrr[299];
char temp;
int k;
int start;
int end;
int max;
k = 0;

if(sz]1000)
sz = 1000;

end = sz;
max = 1;
ttt = rand()%100;
for(int j=0;j[max;j++)
{
srand(rand()*rand()*rand()*rand()*time(NULL));
ttt = abs(rand()%10000);
for(int i=0;i[end;i++)
{
k = k+1;
ttt = ttt*111;
if (ttt[0)
{
ttt=ttt*-1;
end = sz-k;
itoa(ttt,sss1,10);
ttt = k*ttt;
break;
}
}

if (max==1)
{
strcpy(sss,sss1);
}
else
{
strcat(sss,sss1);
}

if(strlen(sss)[sz+rand()%100)
{
max = max+1;
}
}

end = strlen(sss);
start = floor((end-sz)/2);
if (start[0)
start = 0;
if (sz]end)
sz = end;
k = 0;
for(int i=start;i[start+sz;i++)
{
rrr[k] = sss[i];
k++;
}
rrr[sz] = '\0';
return rrr;
}


0

Response Number 3
Name: Don Arnett
Date: November 11, 2002 at 23:33:12 Pacific
Reply:

Programming tip #1 - use meaningful variable names. Variable names like a, b, c, rrr went out of style 15 years ago.

I see two problems.

1 - you are returning 'rrr' which I assume contains a string. But 'rrr' is a local variable (meaning it's declared inside it's function). When get_word() returns control to main(), rrr no longer exists.

2 - Sorry, not two problems. You are returning the address of the array rrr, and storing that address in the array of char pointers errz. So that isn't a problem.

The first step is to make 'rrr' a static local variable (or you could make it a global variable, static local is better):

static char rrr[299];

This causes it to not disappear between function calls (although it can still be accessed by name only from inside the function).

But then your problem will be that you always return the address of 'rrr' (which now doesn't disappear between calls) and store that address in to succeeding elements of 'errz'. So you'll have X elements of errz pointing to 'rrr'. Since the address of rrr never changes, you'll have X pointers to the same string (whichever string was put in rrr last).

So rather than saving the address of rrr, you want to make a copy of the contents of rrr and save the copie's address in errz.

errz[j] = strdup(tmp);

strdup() makes a copy of the passed string by getting the length, allocating memory and copying the string into the new memory.

You include either string.h or strings.h to use it.

To be nice and neat, you want to free up all that allocated memory by looping thru errz and calling free(errz[i]) for each element. This frees each chunk of memory.

But the memory is also freed when you exit the program, so it depends on your program if it's worth the effort to do this.


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: VC++ array of strings

an array of strings in C www.computing.net/answers/programming/an-array-of-strings-in-c/7230.html

Array of Strings www.computing.net/answers/programming/array-of-strings/12524.html

Two dimensional array of strings www.computing.net/answers/programming/two-dimensional-array-of-strings/681.html