HOw to combine a String with an Integer

This is a discussion on HOw to combine a String with an Integer within the C Programming forums, part of the General Programming Boards category; Hi Folks I have a question...and I have tried several ways to solve this...but everything went on in vain. How ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    3

    Unhappy HOw to combine a String with an Integer

    Hi Folks

    I have a question...and I have tried several ways to solve this...but everything went on in vain.

    How can I concatenate a String and an Integer in c.I need to have string say Ravi and an integer say i=1.

    I have to find a way to declare a variable called Ravi1.How to do this???

    I wrote the following Code..
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    main()
    {
    int i=0;
    char *s="Ravi";
    for(i=0;i<10;i++)
    {
    char *strcat(s,itoa(i))="Hai";
    }
    }
    But things are not working well!!!

    So can some one help me solve this!!!!
    }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Not sure what the = "Hai" and char * are doing around the strcat, but:
    Code:
    strcat(s,itoa(i));
    would work if you use char s[NN] = "Ravi", where NN is big enough to hold the string "Ravi", the length of the number and the terminating zero, so 16 should be OK.

    I personally prefer
    Code:
    sprintf(s, "Ravi%d", i);
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    3
    Well its not about printing...

    I need to dynamically declare some pointer variables of type car(Car is a user defined structure)...

    I need to have the Pointer Variables as...

    struct car *s1
    struct car *s2
    ------
    -----
    ---
    --
    struct car *s10.

    So for this purpose I thought to declare pointer of type struct car *s and append it with 1,2,3.............10 using a for loop and even allocate space for it using malloc with in the loop..

    But It was not so easy as I thought????

    Any Ideas..and Thanks for ur quick response..!!

  4. #4
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by matsp View Post
    Not sure what the = "Hai" and char * are doing around the strcat, but:
    Code:
    strcat(s,itoa(i));
    would work if you use char s[NN] = "Ravi", where NN is big enough to hold the string "Ravi", the length of the number and the terminating zero, so 16 should be OK.
    [/code]
    in my case it didn't work because itoa takes 3 arguments.
    Code:
    itoa(i,s,base);
    here i is the integer to be converted and after conversion this string gets stored in s.base is the base of the integer like decimal,binary,octal etc.
    so this will work

    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<string.h>
    int main(void)
    {
    	int i=1;
        char s[16]="Ravi";
    char s2[16];
        strcat(s,itoa(i,s2,10));
    	printf("%s",s);
    	getch();
    	return 0;
    }
    although with 1 argument in itoa(i) it didn't show any error but the result was abnormal.

  5. #5
    Registered User
    Join Date
    Apr 2009
    Posts
    3
    @BEN10

    With the CODE that You have given it gave me the following error.,

    [centos@localhost PRT_Version2]$ cc temp1.c
    temp1.c: In function ‘main’:
    temp1.c:9: warning: passing argument 2 of ‘strcat’ makes pointer from integer without a cast
    /tmp/cc2tOvFw.o: In function `main':
    temp1.c.text+0x58): undefined reference to `itoa'
    collect2: ld returned 1 exit status
    [centos@localhost PRT_Version2]$

    Actually I am using the CENTOS OS.....is there any difference with the version of the C-Compilers???

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Ah, sorry, yes, itoa does indeed take more parameters - I never use itoa, as it is so rare that I need to do this, and sprintf() is far more flexible.

    And to clarify, sprintf is not a "printing function" - it produces a string, using the formatting that printf uses for output.

    But you are barking up the entirely wrong tree:
    If you need multiple pointers, you either use an array of pointers, or a dynamically allocated storage of pointers (held by a pointer to pointer). To dynamically "paste" together a number on a variable is possible in some languages, but C is not one of those [at least, not at runtime, you can have some fun with macros to paste various bits together, but I don't think that would work for what you are describing].

    Also, any time you get something like:
    Code:
    T name1;
    T name2;
    ...
    T nameNN;
    it is almost ALWAYS an indication that you should use an array of type T. Exceptions apply if you have less than 4, in some special cases.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  2. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 02:02 AM
  3. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 10:11 AM
  4. Another overloading "<<" problem
    By alphaoide in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2003, 11:32 AM
  5. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM

Tags for this Thread


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