1

This is my code and when I run it, I am getting segmentation fault.

 char *s = NULL;
s = (char *)malloc(5*sizeof(char));

s[0]='10';

s[1]='20';

printf("%s",s[1]);

please tell where the error is and why this is happening. My intension of the program is to dynamically create a string, give it some value byte by byte and print the values byte by byte.

Is there any way to add integer values to string. Because, I have a situation where length of string is to be in first part of string followed by data. Kindly suggest any method to do this in C.

mit23
  • 53
  • 6

4 Answers4

2

You are storing multibyte characters in s[0] and s[1]. Beside of this use %c format specifier to print a character.

haccks
  • 104,019
  • 25
  • 176
  • 264
  • And printing a `char` with `%s`. – David Ranieri Feb 23 '15 at 06:50
  • @MattMcNabb, what is not wrong, the multibyte assignment or the `printf` part? – David Ranieri Feb 23 '15 at 07:03
  • 1
    sorry, thought we were in c++ for some reason. vote redacted :) – M.M Feb 23 '15 at 07:06
  • 1
    @MattMcNabb, in C it is implementation-defined behavior, **J.3.4** `The value of a wide character constant containing more than one multibyte character, or containing a multibyte character or escape sequence not represented in the extended execution character set.` What about C++? – David Ranieri Feb 23 '15 at 07:17
2

printf("%s", s[1]); means print string starting at address (int)'20'. That's clearly a bug. Should be: s[2]='\0'; printf("%s", s+1 /*&s[1]*/);

Matt
  • 13,674
  • 1
  • 18
  • 27
  • Okay, I got it. My problem is to pack the string with length and followed by data. for eg: "5hello". How to do this in C? – mit23 Feb 23 '15 at 07:04
  • @mit23 To emulate Pascal-string? What for? You will be unable to use C-library "as is". For example, simply to print such string: `for(i=1,j=s[0]; j>0; i++, j--) printf("%c", s[i]);` - isn't it ugly? – Matt Feb 23 '15 at 07:10
  • @user4419802 true, but the simpler `for(i=1; i<=s[0]; i++) printf("%c", s[i]);` is less ugly. – Weather Vane Feb 23 '15 at 07:45
  • @WeatherVane OK, my code was a bit sloppy. BTW, it's better not to use `printf` at all: `for(i=1; i<=s[0]; i++) putchar(s[i]);` – Matt Feb 23 '15 at 08:02
1

1.s[0] is a char you can not assign 10 to it. (It will compile with warnings but it is not doing what you have expected)

2.printf("%s",s[1]); will also cause undefined behavior as string followed bys[1] is not null terminated.

Vagish
  • 2,520
  • 19
  • 32
  • actually with all the long responses here , this is the most accurate and short answer to the original OP question . you got my upvote – David Haim Feb 23 '15 at 12:58
0

Don't cast the result of malloc and family.

s[0] and s[1] will take only one byte value.

Then your string is not a null terminated string.

Try this.

  s[0]='h';
  s[1]='i'; 
  s[2]='\0'; 

  printf("%s",s);

%s is for printing the string. Use %c for printing the character.

  printf("%c",s[1]);

You can use like this to store the integer into an char array,

 char *s=malloc(123);
 sprintf(s,"%d",integer_value);

Then use strcat to store the string in that.

Passing to the function use like this,

   void func(char *);
   main() { 
      . . .
       func(s);
      . . .
   }
   void func(char *str)
    { 
         // do what you need
    }

if between integer and string slash is available ( like in your comment) you can use the strtok function. Before using the strtok function take backup for your string. It will modify your content. Or els you can use like this,

  sscanf(str,"%d / %s",&int,string);

Now string have the value "hello".

  char s[10];
  sprintf(s,"%d",int);
  strcat(str,s);

Try this,

    int fd;

    char str[]="data7";
    char st[12];

    sscanf(str,"%[^ 0-9] %d",st,&fd);
    printf("st:%s\tfd:%d\n",st,fd);   
Karthikeyan.R.S
  • 3,991
  • 1
  • 19
  • 31