Appending a char to a char*?

This is a discussion on Appending a char to a char*? within the C Programming forums, part of the General Programming Boards category; Hello, i really need help. I want to append a single char into a char* that i have..how do i ...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,535
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,535
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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
    21,717
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 04: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-04-2009, 11:53 PM
  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, 03:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21