From: KURT P. <pet...@ms...> - 2011-07-02 17:19:53
|
> Date: Sat, 2 Jul 2011 12:58:36 -0400 > From: ea...@us... > To: min...@li... > Subject: Re: [Mingw-users] Access violation using string - don't know why > > KURT PETERS wrote: > > > > This code compiles, but I get an error when running the code in the uppercase loop. > > Can someone tell me why I'm getting an error. I'm trying to follow this example: > > http://www.java2s.com/Code/C/String/Changestringcaseusingitspointer.htm > > [code] > > typedef struct configData configData; > > struct configData { > > char* typetranslation;// = "fwd"; > > }; > > char* p; > > configData *cd; > > int ii; > > cd->typetranslation = "rev\0"; /* NOTE:added \0 later after receiving access violations. shouldn't matter */ > > > > p=cd->typetranslation; > > printf("%s\n",p); > > > > printf("%s\n",cd->typetranslation); > > /* both of the above print rev */ > > /* error somewhere in code below */ > > for (ii=0;ii<3;ii++) { > > *p = toupper(*p); > > p++; > > } > > printf("%s\n",cd->typetranslation); > > [/code] > > Your typetranslation pointer points to a string constant which cannot be > modified and thus the error. If you set the string to a char variable > and not a pointer then you should be able to do what you want. > > -- > Earnie Thanks Earnie, Since I'm not sure how to actually make it a char variable, I did this, which worked. Is there an easier way I should have used? [code] cd->typetranslation = (char *)malloc(strlen("rev")); if (cd->typetranslation == NULL) { return 0; } strcpy(cd->typetranslation,"rev"); [/code] Kurt |