-6
# include <stdio.h>
int main(void)
{
  char a[]="0123456789";
  char b[5];

  a[5]='\0';
char *c=a;

  for(int i=0,j=sizeof(b)/sizeof(char);i<j;i++){

    *(b+i)=*(c+(7*i));
  }
  
printf("%d\n", sizeof(a)/sizeof(char));
  printf("%s\n", a);
  printf("%s\n", b);
  printf("%c\n", *(b+3));

  return 0;
      }

There is no one output in this code 4 outputs are needed but one output is missing and I don't know why it happens and how to solve this problem

Hallo
  • 1
  • 2
    Welcome to SO. For such tiny programs it would be a good start to just run it in a debugger, step through each instruction and inspect what your variables hold. Where do your pointers point to, what array elements do you modify, etc.? – Gerhardh Jun 07 '22 at 13:33
  • Please specify exactly the actual output and the desired output. – Andreas Wenzel Jun 07 '22 at 13:33
  • 2
    Have you tried running your code line by line in a debugger while monitoring the values of all variables, in order to determine in which line your program stops behaving as intended? If you did not try this, then you may want to read this: [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/12149471) You may also want to read this: [How to debug small programs?](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – Andreas Wenzel Jun 07 '22 at 13:34
  • 1
    `*(c+(7*i))` is more commonly written as `c[7*i]`. When `i >= 2` ... `c[7*i]` does not exist. – pmg Jun 07 '22 at 13:34
  • Unless your program crashes, I can't see how you would get "no one output". BTW: `sizeof` evaluates to type `size_t` which is printed using format specifier `%zu`, not `%d`. – Gerhardh Jun 07 '22 at 13:35

1 Answers1

1

Let me walk you through what is happening.

I've modified your code slightly to make it easier to read

#include <stdio.h>

int main(void)
{
  char a[]="0123456789"; //a is 11 chars long [0,1,2,3,4,5,6,7,8,9,\0]
  char b[5];             //b is 5 chars long and we don't know what's in it

  a[5]='\0';             //a now looks like this [0,1,2,3,4,\0,6,7,8,9,\0]
  char *c=a;             //c points to a

  for(int i=0; i < sizeof b / sizeof(char); i++){  //j is unnecessary here
    b[i] = c[7 * i]; //This is equivalent to your code and is easier to read
  }

  //If the program hasn't crashed, b now looks like this [0,7,?,?,?]
  //(where ? means it could be anything at all)

  printf("%zd\n", sizeof a /sizeof(char)); //We expect 11 to print (%zd is for size_t)
  printf("%s\n", a);                       //Strings end at \0 so we expect "01234"
  printf("%s\n", b);                       //We expect "07..." but we have no idea what comes next
  printf("%c\n", b[3]);                    //This could do anything

  return 0;
}

In your for loop, you initialize the 5-character array b with c[0], c[7], c[14], c[21], and c[28].

You initialized c by pointing it to a, so c[0] is the same as a[0], c[7] is the same as a[7] etc.

Since a is only 11 characters long, we can be certain that b[0] = '0' and b[1] = '7' but after that we've invoked undefined behavior and we have no idea what will happen. It is entirely possible that the program will crash after printing...

11
01234
07

or it could do something completely unexpected.

Willis Hershey
  • 1,520
  • 4
  • 22