By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,125 Members | 1,249 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,125 IT Pros & Developers. It's quick & easy.

String problem in C

P: 11
Dear all
I'm new here and also new to C language. I have been stuck with string problem in C for some time and I decide to post here to get some help, please kindly explain what I get wrong with the code

char *ptr, *old2, *oldstring);

printf("\nEnter oldstring expression :");
gets(oldstring);
printf("\n%s : old2", old2);
ptr = strcpy(old2, oldstring);
printf("\n%s : old2", ptr);
printf("\n%s : old2", old2);

Seems like when I entered oldstring, the program tends to freeze, I assume the problem is "segmentation error" but I'm not sure.. Please anyone tell me how to copy the value of string to another string, I have used

a = oldstring;

provided a and oldstring is char*
a value tend to change even after that I change value of oldstring.. I think it's because a is just a pointer isn't it?

Thank you very much for all answers
Jul 12 '06 #1
Share this Question
Share on Google+
8 Replies


P: 35
Hi,
I couldn't understand your question but I think the problem with your code is that you have not initialised the pointers (oldstring, old2, etc) and without initialising them you cannot assign values to them.
So assign addresses of variables as : oldstring = &x;
Check this out if it solves your problem or not.
Jul 13 '06 #2

P: 2
Here is a simple program to copy the contents of one string to another.
main()
{
char src[10]; //source string
char tar[10]; //target string means the string which is going to contain result
char * ptr1;
char *ptr2;
printf("Enter the source string\n");
gets(src);
ptr1 = src; //ptr1 contains the base address of source string
ptr2 = tar; // ptr2 contains the base address of target string
while( *ptr1 != '\0')
*ptr2++ = * ptr1++;
printf("The target string is %s", tar);
}
This is one of the simplest way to copy the contents of 1 string to other.
Jul 13 '06 #3

Banfa
Expert Mod 5K+
P: 8,916
Actually one of the simplest ways of copying 1 string to another is strcpy

Expand|Select|Wrap|Line Numbers
  1.    char string1[10] = "Hello";
  2.    char string2[10];
  3.  
  4.    strcpy(string2, string1);
  5.  
Ashish_CPP is on the right lines, you have used pointers but you have used them without pointing them at anything, a pointer has to be allocated data before you can assign any values to it.

Expand|Select|Wrap|Line Numbers
  1.    char string1[10] = "Hello";
  2.    char *string2;
  3.  
  4.    string2 = malloc(strlen(string1)+1);
  5.  
  6.    if (string2 != NULL)
  7.    {
  8.       strcpy(string2, string1);
  9.    }
  10.  
If you copy pointers then they will both point to the same data so modifying the data 1 points at will affect them data the other points at (as it is the same data).

Expand|Select|Wrap|Line Numbers
  1.    char string1[10] = "Hello";
  2.    char *string2;
  3.    char *string3;
  4.  
  5.    string2 = malloc(strlen(string1)+1);
  6.  
  7.    if (string2 != NULL)
  8.    {
  9.       strcpy(string2, string1);
  10.    }
  11.  
  12.    string3 = string2;
  13.  
  14.    string3[0] = '\'';
  15.  
  16.    printf( "%s\n", string2 );
  17.  
this outputs

'ello



NOTE: If you do use malloc then you should free the data when finished
Jul 13 '06 #4

P: 11
Thank you very much, I'll see if that helps
Jul 13 '06 #5

P: 2
Sometime if you not allocate memory for these strings you may got troble SO allocate memory properly And delete properly
Jul 15 '06 #6

P: 11
Dear all,
I've tried to allocate memory and I still got problem, could u please look at my program in the following post

It's been giving error "Segmentation Error" message, I really don't know what is going wrong

Thank you
Jul 15 '06 #7

P: 11
Expand|Select|Wrap|Line Numbers
  1. //This procedure take in normal string and convert to single character rep
  2. //such as "(img1.tga over img2.tga) in img.tga" -> "(aOb)Ic"
  3. //And store image filename in string variables
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8.  
  9. int oldtonew(char *oldstring,char *newstring){
  10.     int i,j,k,l,count,size;
  11.     char *tempstring, *O, *I, *U, *A, *X, *a, *b, *c;
  12.     char *img[2];
  13.     count=0;
  14.     i=0;
  15.     j=0;
  16.     k=0;
  17.     l=0;
  18.  
  19.     size=strlen(oldstring);
  20.  
  21.     while (i<size){
  22.     if ((oldstring[i]=='(')||(oldstring[i]==')')){
  23.         newstring[k]=oldstring[i];
  24.         k++;
  25.         i++;
  26.         }
  27.     else if (oldstring[i]==' '){
  28.         i++;
  29.         }
  30.     else{
  31.         j=0;
  32.         while ((oldstring[i]!=' ')&&(oldstring[i]!=')')
  33.         &&(oldstring[i]!='(')&&(i<=size)){
  34.             tempstring[j]=oldstring[i];
  35.             j++;
  36.             i++;
  37.         }
  38.  
  39.         tempstring[j]='\0';
  40.         printf("tempstring=::%s::\n",tempstring);
  41.         if (strcmp(tempstring,"over")==0) 
  42.             {
  43.             O = "over";
  44.             newstring[k]='O';
  45.             k++;
  46.             }
  47.         else if (strcmp(tempstring,"in")==0){
  48.             I = "in";
  49.             newstring[k]='I';
  50.             k++;
  51.             }
  52.         else if (strcmp(tempstring,"out")==0){
  53.             U = "out";
  54.             newstring[k]='U';
  55.             k++;
  56.             }  
  57.         else if (strcmp(tempstring,"atop")==0){
  58.             A = "atop";
  59.             newstring[k]='A';
  60.             k++;
  61.             }      
  62.         else if (strcmp(tempstring,"xor")==0){
  63.             X = "xor";
  64.             newstring[k]='X';
  65.             k++;
  66.             }
  67.         else
  68.             { 
  69.             printf("\ndebug5");
  70.             img[l] = malloc((strlen(tempstring))+1);
  71.  
  72.             if (img[l] != NULL)
  73.                {
  74.                strcpy(img[l],tempstring);
  75.  
  76.                newstring[k]=l;
  77.                }
  78.             img[l][strlen(img[l])]='\0';
  79.             l++;
  80.             k++;
  81.             }
  82.       } 
  83.     }
  84.     //img[0][strlen(img[0])]='\0';
  85.     //img[1][strlen(img[1])]='\0';
  86.     //img[2][strlen(img[2])]='\0';
  87.     //printf("string img[0] = ::%s::\n", img[0]);
  88.     //printf("string img[1] = ::%s::\n", img[1]);
  89.     //printf("string img[2] = ::%s::\n", img[2]);
  90.     newstring[k]='\0';
  91. }
  92.  
  93. int main() 
  94. {
  95.  
  96.     char *oldstring;
  97.     char newstring[200];
  98.     char *old2, *ptr;
  99.     int size;
  100.     printf("\nEnter oldstring expression :");
  101.     gets(oldstring);
  102.     printf("\n%s : oldstring", oldstring);
  103.     oldtonew(oldstring,newstring);
  104.     printf("\nnewstring: ::%s::\n",newstring);
  105.     printf("\ndebug");
  106.     system("PAUSE");    
  107.     return 0;
  108. }
  109.  
  110.  
Jul 15 '06 #8

Banfa
Expert Mod 5K+
P: 8,916
Expand|Select|Wrap|Line Numbers
  1. //This procedure take in normal string and convert to single character rep
  2. //such as "(img1.tga over img2.tga) in img.tga" -> "(aOb)Ic"
  3. //And store image filename in string variables
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8.  
  9. int oldtonew(char *oldstring,char *newstring){
  10.  
  11.     <snipped>
  12. }
  13.  
  14. int main() 
  15. {
  16.  
  17.     char *oldstring;
  18.     char newstring[200];
  19.     char *old2, *ptr;
  20.     int size;
  21.     printf("\nEnter oldstring expression :");
  22.     gets(oldstring);
  23.     printf("\n%s : oldstring", oldstring);
  24.     oldtonew(oldstring,newstring);
  25.     printf("\nnewstring: ::%s::\n",newstring);
  26.     printf("\ndebug");
  27.     system("PAUSE");    
  28.     return 0;
  29. }
ignoring any errors that might exist in oldtonew in main you are still using the variable oldstring which is a char * which supplying any memory for it. Also rather than gets use fgets, this allows you to protect your program from buffer overrun (where you write more data to the buffer than it has storage for.

If I were you I would try

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. int main(int argc, char**argv) 
  6. {
  7.  
  8.     char oldstring[200];
  9.  
  10.     <snipped>
  11.  
  12.     fgets(oldstring, sizeof oldstring, stdin);
  13.  
  14.     <snipped>
  15.  
  16.     return 0;
  17. }
Jul 17 '06 #9

Post your reply

Sign in to post your reply or Sign up for a free account.