strcat problem

This is a discussion on strcat problem within the C Programming forums, part of the General Programming Boards category; Code: for(int i =0; i<boardCount; i++) { char boardName[] = "dxxxB"; strcat(boardName,i); } how can I add the integer to ...

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    18

    strcat problem

    Code:
    for(int i =0; i<boardCount; i++)
    {
    	char boardName[] = "dxxxB";
                    strcat(boardName,i);
    }
    how can I add the integer to the end of string and let compiler interpret it as a whole big char array? Pls help me and thank you in advance

  2. #2
    Registered User
    Join Date
    Dec 2005
    Posts
    167
    check sprintf(...);

  3. #3
    Registered User
    Join Date
    Aug 2007
    Posts
    18
    Even if I look sprintf it seem like only formating strings and do not return them I have to return DXXXB1, DXXXB2 and pass them to a function as reference, could you help me?

    Also getting errors cannot convert int to constant char, is there anything else such as append string? I am very unfamiliar with C thank u in advance
    Last edited by mindtrap; 08-10-2007 at 03:15 AM.

  4. #4
    Registered User
    Join Date
    Dec 2005
    Posts
    167
    is this something that you need?

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    
    char *func()
    {
            char *array=calloc(100,sizeof(char));
            int i;
    
            i=1;
            sprintf(array,"DXXXB&#37;d",i);
    
            return array;
    }
    
    
    
    int main()
    {
            char *p;
    
            p=func();
            puts(p);
            free(p);
    
            return 0;
    }

  5. #5
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    You can't pass by reference in C, however you can pass a pointer.

    you'll have to resize boardName (it's allocated on the stack), since it's not large enough to hold anymore than "dxxxB"+1 characters, either make it big enough, or use dynamic memory allocation.

    such as:
    Code:
    #include <stdio.h>
    
    void foo(char * s);
    
    int main(void)
    {
        char boardName[32];
        size_t i;
    
        for(i = 0; i < boardCount; i++)
        {
            sprintf(boardName, "dxxxB&#37;d", i);
            foo(boardName);
        }
    
        return 0;
    }
    
    void foo(char * s)
    {
        /* do whatever with s */
        return;
    }
    Last edited by zacs7; 08-10-2007 at 03:38 AM.

  6. #6
    Registered User
    Join Date
    Aug 2007
    Posts
    18
    Thanx you. Both of them work and I learnt more than I expect.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. STRCAT problem
    By Frusciante in forum C Programming
    Replies: 9
    Last Post: 07-04-2007, 07:15 AM
  2. Problem with strcat
    By firyace in forum C Programming
    Replies: 9
    Last Post: 05-15-2007, 03:31 PM
  3. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 07:54 PM
  4. Problem with strcat() function
    By sureshkumarct in forum C Programming
    Replies: 6
    Last Post: 01-03-2007, 08:05 PM
  5. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM

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