Hello I need to write a used defined function through which I need to extract specified no of characters, Although I am able to do this but I have one doubt through which I am not getting the expected o/p.
I used the following code which gives me the expected o/p
#include <stdio.h>
int xleft(const char *s, char *t, int offset)
{
int i;
for(i=0;i<offset;++i)
{
*(t+i)=*(s+i); // t[i]=s[i] also worked which I guess is the
//syntactical sugar for it. Am I rt ?
}
t[i+1]='\0';
return 1;
}
int main()
{
char mess[]="Do not blame me, I never voted VP";
char newmess[7];
xleft(mess,newmess,6);
puts(newmess);
return 0;
}
But I am not able to understand why I am not getting the o/p when I write the code like this
#include <stdio.h>
int xleft(const char *s,char *t, int offset)
{
int i;
for(i=0;i<offset;++i)
{
*t++=*s++;
}
t[i+1]='\0';
return 1;
}
int main()
{
char mess[]="Do not blame me, I never voted VP";
char newmess[7];
xleft(mess,newmess,6);
puts(newmess);
return 0;
}
offset + 1times? (hence, you're writing into the output array out-of-bounds in both cases.)a[index], ifais an array, is*(&a[0] + index).