Thread: Appending a char to a char*?

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    32

    Appending a char to a char*?

    Hello, i really need help. I want to append a single char into a char* that i have..how do i do that? Is it even possible?

    Code:
    char temp[5] ="";
    
    while(isalnum(*x))
    {
    	strcat(temp,*x);
    	x++;
    }
    enqueue(&head,temp);
    Seems that strcat isn't possible for this, i get an error when i run this.

    The situation is i have a Queue which has a char* as a variable, because i want to store some string there. And then i have a char* to be read, and if the char it is pointing to is alphanumeric, i want it to append to temp and after all that, enqueue it.

    Help guys?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Normally, something like this (assuming temp will always be large enough - if not, add some checks).
    Code:
    char temp[5] ="";
    char *p = temp;
    while(isalnum(*x))
    {
        *p++ = *x++;
    }
    *p = '\0;
    enqueue(&head,temp);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    32
    I'll be using this for small numbers, so i guess temp[5] will be large enough. Can you explain what happens in your code?

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by mugiwara528 View Post
    The situation is i have a Queue which has a char* as a variable, because i want to store some string there. And then i have a char* to be read, and if the char it is pointing to is alphanumeric, i want it to append to temp and after all that, enqueue it.
    Help guys?
    Since this isn't what I'd call a very clear description your best bet would be to rough out some code, even if it's not working and post it up and we'll see what we can do...

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Can you explain what happens in your code?
    Maybe, but since you're already able to write enqueue() as well, it should be pretty easy for you to study.

    Well at least longer than the maximum of 4 minutes it took from my reply to yours for you to resort to posting a question.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Mar 2011
    Posts
    32
    Quote Originally Posted by CommonTater View Post
    Since this isn't what I'd call a very clear description your best bet would be to rough out some code, even if it's not working and post it up and we'll see what we can do...
    Code:
    struct node  //STACK
    {
        char *x;
        struct node* next;
    };
    
    struct nd  //QUEUE
    {
        char *x;
        struct nd* next;
    };
    
    void enqueue(QUEUE *head, char *c)
    {    
        QUEUE p;
        QUEUE temp;
        
        temp = (queues*) malloc(sizeof(queues));
    
        if(*head==NULL)
        {
            temp->x = (char*)calloc(strlen(c),sizeof(char));
            temp->next = NULL;
            *head=temp;
        }
        
        else
        {
            p=*head;
            while(p->next!=NULL)
                p=p->next;
    
            temp->x =(char*)calloc(strlen(c),sizeof(char));
            temp->next = NULL;
            p->next = temp;    
        }
    }
    
    void toRPN(STACK *top, char* x, STACK2 *top2)
    {
    	setColor(2);
    
        while(*x!='\0')
        {
            if(*x=='(')
                push(&(*top),*x);
    
            else if(*x==')')
            {
                while(!isEmpty(*top) && strcmp((*top)->x,"(")!=0)
                    pop(&(*top));
                
                if(strcmp((*top)->x,"(")==0)
                    pop(&(*top));
            }
    
            else if(isalnum(*x))
    		{
    			char temp[5] ="";
    			char *p = temp;
    
    			while(isalnum(*x))
    			{
    				*p++ = *x++;
    			}
    
    			*p='\0';
    
                enqueue(&head,temp);
    		}
    ...
    Is that okay? I modified it with Salem's code already. I got rid of that error but now im getting another one -__-

  7. #7
    Registered User
    Join Date
    Mar 2011
    Posts
    32
    Quote Originally Posted by Salem View Post
    > Can you explain what happens in your code?
    Maybe, but since you're already able to write enqueue() as well, it should be pretty easy for you to study.

    Well at least longer than the maximum of 4 minutes it took from my reply to yours for you to resort to posting a question.
    Thank you, i think i get it now. I'll try to see if this goes well with my whole program.

  8. #8
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    You can take strncat like
    Code:
    char temp[5] ="";
    
    while(isalnum(*x))
    {
    	strncat(temp,x,1);
    	x++;
    }

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by BillyTKid View Post
    You can take strncat like
    Code:
    char temp[5] ="";
    
    while(isalnum(*x))
    {
    	strncat(temp,x,1);
    	x++;
    }
    Now, BillyTKid's suggestion that I quoted above is very much based on the code in your post #1, and realistically with temp being a char[5] it is okay. However, in other situations, such a solution is needlessly inefficient. For a better understanding why, read Joel Spolsky's essay entitled Back to Basics.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Appending char to C-Style string
    By Yinyang107 in forum C++ Programming
    Replies: 5
    Last Post: 02-07-2011, 05:22 PM
  2. Appending to the end of a char*
    By thegr8n8 in forum C++ Programming
    Replies: 44
    Last Post: 08-12-2010, 03:28 PM
  3. Appending a char to a char *foo;
    By nightowl09 in forum C Programming
    Replies: 5
    Last Post: 02-05-2009, 12:53 AM
  4. Appending char to string (char array)
    By sniper83 in forum C Programming
    Replies: 14
    Last Post: 04-15-2008, 06:48 AM
  5. Appending charaters to a char*
    By masterblix in forum C++ Programming
    Replies: 3
    Last Post: 11-22-2006, 04:04 PM