0

I am trying to format and merge the strings (with sprintf) and then print them with printf. But it does not work, and I have no clue why. The error is that string is not initialised.

int main() {
char wochentag[] = "Freitag";
int tag = 13;
char monat[] = "Mai";
int jahr = 1927;
char *string;
char *array=(char *) malloc(26*sizeof(char));
sprintf (string,"%s" "%d" "%s" "%d",wochentag,tag,monat,jahr);


printf("%s\n", string);
free(array);

return 0;
}
Fabian Mensch
  • 17
  • 1
  • 5
  • 2
    I don't see a question, but it looks like you need to enclose your format specifier in quotes. And `(* char)` is not a valid cast. – owacoder Oct 24 '15 at 15:32
  • http://ideone.com/T9Cnxo – BLUEPIXY Oct 24 '15 at 15:39
  • 1
    @Fabian - `array` is allocated but never used. `string` is used but never allocated. You see the problem? – owacoder Oct 24 '15 at 15:41
  • Okay guys, thank you I finally got it! – Fabian Mensch Oct 24 '15 at 15:41
  • 1
    You should not cast the return value of malloc: http://stackoverflow.com/q/605845/1025391 – moooeeeep Oct 24 '15 at 15:53
  • `char monat[] = "Mai"` is a worse choice than `const char *monat = "Mai"`, because the first declares a non-const char array. The second can keep the string data in read-only memory, sharing storage with any other uses of the same string constant. – Peter Cordes Oct 24 '15 at 19:19
  • regarding the call to malloc(): the expression: `sizeof(char)` is defined by the standard as always 1. Multiplying anything by ` results in no change. Therefore the expression just clutters the code. When calling malloc(), always check (!=NULL) the returned value to assure the operation was successful – user3629249 Oct 26 '15 at 12:14
  • Where did the magic number 26 come from anyway? You should replace it with some meaningful constant. – Lundin Oct 26 '15 at 12:31

2 Answers2

1

The following fixes achieve what you're trying to do:

char *array=(char *) malloc(26*sizeof(char));

A pointer to char is char*, not *char.

char *array=(char *) malloc(26*sizeof(char));
sprintf (array,"%s %d %s",wochentag,tag,monat);
printf("%s\n", array);

Since you allocate memory to your array variable, that's what you should use in sprintf and printf, right? Also note that the correct use of sprintf is with quotation marks.

This is the fixed code:

int main() {
    char wochentag[] = "Freitag";
    int tag = 13;
    char monat[] = "Mai";
    int jahr = 1927;
    char *string;

    char *array=(char *) malloc(26*sizeof(char));
    sprintf (array,"%s %d %s",wochentag,tag,monat);

    printf("%s\n", array);
    free(array);

    return 0;
}
Ismael Padilla
  • 5,246
  • 4
  • 23
  • 35
0

the following code compiles cleanly, removes code clutter, performs error checking, includes the needed header files, is appropriately indented for readability and works correctly.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


int main()
{
    char wochentag[] = "Freitag";
    int tag = 13;
    char monat[] = "Mai";
    int jahr = 1927;

    char *string=malloc(26);
    if( NULL == string)
    { // then malloc failed
        perror( "malloc for 26 bytes failed");
        exit( EXIT_FAILURE);
    }

    // implied else, malloc successful

    sprintf (string,"%s%d%s%d",wochentag,tag,monat,jahr);
    printf("%s\n", string);
    free(string);

    return 0;
}
user3629249
  • 16,402
  • 1
  • 16
  • 17