How to convert int type string to char?

I have code like following:

1
2
3
4
5
6
int something[3];
something[0] = "some ";
something[1] = "text";
something[2] = '\n';

char str[3] = "abc";


Now how do i convert something[1] to string so I could use strcat(); to merge something[1] and str?
You can't assign string literals to ints ...
well what i posted above didnt give me any errors. I am using codeblocks as IDE. And it's pure C.

Edit: You can do that only in pure C. And I also found how to do what I want.

Look what happens in main(). and that code also compiles. and also works (with codeblocks).
For some reason it doesnt work with MS VC++.

http://www.codeblocks.org/downloads/26 - I use this: codeblocks-10.05mingw-setup.exe

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
//kuna C ei toeta booli peab booli ise defineerima.
#define bool int
#define true 1
#define false 0

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int MaximaalneFailiTeekond = 260;
// küll windows juba sele eest hoolitseb et teekond pikem ei saaks olla

/*=================================================;
Funktsioon............: LoeFail
Selgitus..............: Loeb faili ning tagastab selle sisu
Kasutus...............: LoeFail(char sTeekond)
Parameetrid...........: sTeekond - Teekond failini mida lugeda
Tagastatavad väärtused: Edukorral - Faili sisu
                        Ebaõnnestumisel - Tühi tekst
==================================================*/
char* LoeFail(char sTeekond[MaximaalneFailiTeekond])
{
    long lSuurus;
    FILE *pFail;
    char *pRuum;
    pFail = fopen(sTeekond,"r");
    if (pFail==NULL){printf("Ei õnnestunud faili %s avada",sTeekond);return "";}
    //Faili suurus.
    fseek (pFail , 0 , SEEK_END);
    lSuurus = ftell (pFail);
    rewind (pFail);

    //Reserveerime failijaoks ruumi
    pRuum = (char*) malloc (sizeof(char)*lSuurus);
    if (pRuum == NULL) {printf ("Ei õnnestunud mälu reserveerida"); return "";}

    fread (pRuum,1,lSuurus,pFail);
    fclose (pFail);//Sulgeme faili
    return pRuum;
    //Kuidas mälu vabastada? return rikub ürituse ära.
    //free (pRuum);// Vabastame mälu
}

bool KirjutaFail (char sTeekond[MaximaalneFailiTeekond], char * sAndmed)
{
  FILE * pFail;
  pFail = fopen (sTeekond,"w");
  if (pFail!=NULL)//faili avamine õnnestus
  {
    fputs (sAndmed,pFail);
    fclose (pFail);
  }
  return 0;
}

int main()
{
    char * rida = strtok(LoeFail("tel.txt"),"\r\n");
    int i[atoi(rida)+1];
    i[0] = atoi(rida);
    int n = 1;
    int a,b;
        while( rida != NULL )
        {
            rida = strtok( NULL,"\r\n");
            i[n] = rida;
            n += 1;
        }
    for (a = 0;a < i[0]; a++)
    {
        //printf("%d %s\n",a+1,i[a+1]);
        for (b = 0;b < i[0]; b++)
        {
            if (a != b)
            {
                if (strlen(i[a+1]) < strlen(i[b+1]))
                {
                    if (strcmp(i[a+1],i[b+1]) < 0)
                    {
                        char *str1 = "JAH";
                        char *str2 = i[a+1];
                        char *str3 = i[b+1];
                        char str[strlen(i[a+1])+strlen(i[b+1]) +3];
                        strcpy (str,str1);
                        strcat(str,"\r\n");
                        strcat(str,str2);
                        strcat(str,"\r\n");
                        strcat(str,str3);

                        //printf("%s\n",str);
                        KirjutaFail("valjund.txt",str);
                        return 0;
                    }
                }
            }
        }
    }
    KirjutaFail("valjund.txt","EI");
    return 0;
}
Last edited on
In C you can implicitly cast a pointer to an integer but the int but that's a really bad practice. Why don't you make it an array of const char*?
Look my code above, how could I write it better.
This:
1
2
3
4
5
6
int something[3];
something[0] = "some ";
something[1] = "text";
something[2] = '\n';

char str[3] = "abc";

shoult be:
1
2
3
4
5
6
7
const char* something[3];

something[0] = "some ";
something[1] = "text";
something[2] = '\n';

char str[3] = "abc";
Didn't know const keyword. what's diference between const char* and char *?
None. Some, myself included, prefer to use char* to emphasize that the type is pointer to char, not char.

EDIT: oops. The const keyword makes the object read-only. It can't be altered (actually it can, by use of a cast, but that should only be used when you're interacting with code you don't own).
Last edited on
const char* means that the referred array of characters cannot be modified
In C there's not much difference ( you may get segmentation fault but not compiler errors )
C++ forces string literals to be of type const char*
This: char str[3] = "abc";

Should actually be this: char str[4] = "abc";

or this: char str[] = "abc";
what's the diference between char * str= "abc"; and char str[] = "abc";?

and why This: char str[3] = "abc";

Should actually be this: char str[4] = "abc"; ??

is it because string ends with '\0' ? so far I haven't had problem with char str[3] = "abc";
char * str= "abc"; creates a pointers and makes it point to a read-only location of memory containing "abc"
char str[] = "abc"; allocates an auto array of 4 characters and copies "abc" in it ( you can modify the contents of this array )

A "string literal" always contains the nul ( \0 ) character so you should make room for it. If you don't you may get problems if you pass your string to a function expecting it to be nul-terminated
Topic archived. No new replies allowed.