Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

popcorn

arrays of pointers to strings

This topic is 5244 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Can someone explain to me why this doesn''t work.
#include <stdio.h>
#include <stdlib.h>

#define MAXSTRINGS 5

int main(int argc, char *argv[])
{
  char *pArrayString[MAXSTRINGS];
  int i;
  
  strcpy(pArrayString[0], "Hello");
  strcpy(pArrayString[1], "world");
  strcpy(pArrayString[2], "and");
  strcpy(pArrayString[3], "deluxe");
  strcpy(pArrayString[4], "edition");
  
  for(i=0; i<MAXSTRINGS; i++)
    printf("String is: %c\n", pArrayString[i]);
    
  printf("pArrayString is %d\n", sizeof(pArrayString));
  
  system("PAUSE");	
  return 0;
[\source]
How about them apples?

Share this post


Link to post
Share on other sites
Advertisement
That''s a strange way to declare an array of strings. try creating a two dimentional array instead.

char stringarray[10][MAXSTRINGS];

unless that defeats the whole purpose of the tutorial you''re reading...

Share this post


Link to post
Share on other sites
you never allocate memory for the strings numbnuts


const int MAX_STUFF = 5;
char* stuff[MAX_STUFF];
stuff[0] = new char[some_number];
strcpy(stuff[0], "words");
// wash rinse repeat


delete stuff[0];




"A soldier is a part of the 1% of the population that keeps the other 99% free" - Lt. Colonel Todd, 1/38th Infantry, Ft. Benning, GA

Share this post


Link to post
Share on other sites
because you never allocate space for any of the pointers in your array. it''s dangerous to thing of char* variables as strings. they are only kindof. a char* is literally just a pointer to a variable of type char. if you never allocate the memory space to which the pointer points, then you crash. to get what you want, you need something like this:


//create the arrays

char *stringArray[NUM_STRINGS]''
for (int i = 0; i < NUM_STRINGS; i++)
stringArray[i] = new char[DEFAULT_STRING_SIZE];

//then at the very end when you''re done with the strings:

for (int i = 0; i < NUM_STRINGS; i++)
delete[] stringArray[i];


what you want is something more like this:

//std::string class is what you want to use for c strings

std::string stringArray[NUM_STRINGS];


-me

Share this post


Link to post
Share on other sites
Ok, thanks. What I was thinking was that the strcpy function would actually create space in memory for the strings and then I would just need a pointer to that memory. I didn't realise you had ask for the memory explicitly.

[edited by - popcorn on April 9, 2004 9:30:36 PM]

Share this post


Link to post
Share on other sites
ya that''s why using char* as string sucks the big ass. another problem is that strcpy isn''t smart enough to do anything if you don''t allocate enough space for the string:


//this will compile and run fine some of the time

char *foo = new char[1];
strcpy(foo, "I am too big");


with char* variables you ALWAYS have to make sure you have enough space for the string you are storing in the array. if you don''t and you''re lucky, it will crash. if you''re not lucky it will over-write other parts of your application''s memory space and cause all numbers of wierd bugs and errors. the great majority of viruses out in the world, in fact, exploit exactly this kind of coding error to work. the error is called a buffer overrun and if you know some sneaky programming tricks you can use that overrun to exploit the application. so yeah, try and use std::string as much as possible. char* vars are evil.

-me

Share this post


Link to post
Share on other sites
Thanks Palidine, for the detailed explanations. You know I just tried the following code and it worked fine.

Although trying it a few times the results can became unpredictable. When I first seen char * variables used to store strings I thought that it was such a neat trick but now I guess like all things you have to know when to use it and how to use it properly.



char *pChar[2];

strcpy(pChar[0], "hello");
printf("String is: %s\n", pChar[0]);
strcpy(pChar[1], "hell");
printf("String is: %s\n", pChar[1]);
[\source]

How about them apples?


[edited by - popcorn on April 9, 2004 9:56:34 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by popcorn
When I first seen char * variables used to store strings...
That''s exactly the problem. "char * variables" don''t store anything; they point to the beginning (or middle, or end) of a sequence of memory locations, each of which contains a character in the string. The end of the string is denoted by a zero byte, which is why such strings are referred to as null-terminated character arrays. Very subtle, very complex distinctions.

Using the str* family of functions is tricky; make sure you allocate memory, and then make sure you don''t overrun that memory. Many people recommend using the strn* variants (eg strncpy) instead. Unless you have a good reason not to (like, for example, programming in C), I recommend std::string.

Share this post


Link to post
Share on other sites
Thanks for the advice Oluseyi. Can I ask people though when you create a string using a statement like:

char *pString = "hello";

I assume that when you compile a statement like that the compiler creates the string in memory and assigns the address to pString. However is the area of memory that the compiler creates for the string "hello" valid for overwriting. Can you do for example:

strcpy(pString, "hel");

Share this post


Link to post
Share on other sites
quote:
Original post by popcorn
Thanks for the advice Oluseyi. Can I ask people though when you create a string using a statement like:

char *pString = "hello";

I assume that when you compile a statement like that the compiler creates the string in memory and assigns the address to pString. However is the area of memory that the compiler creates for the string "hello" valid for overwriting. Can you do for example:

strcpy(pString, "hel");
You can, but be aware of subtle gotchas. If you use that literal more than once throughout your program, your compiler will likely replace all references/literals with pointers to that one string, which is inserted into the executable''s data segment. Overwriting the string changes it for all instances. Try this as an illustration:

#include <cstring>
#include <iostream>
 
char *str1 = "Monday";
char *str2 = "Monday";
 
int main()
{
using namespace std;
strcpy(str1, "Fri");
 
cout << str2 << endl;
return 0;
}
Build release with optimizations on.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!