0

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;
}
3
  • You do realize the loop executes offset + 1 times? (hence, you're writing into the output array out-of-bounds in both cases.) Commented Jan 29, 2015 at 6:54
  • Well I found this as strange thing that even though the formal parameters in the function were pointer but they can be used as arrays, Isn't it strange cause sometimes it confuses me :| Commented Jan 29, 2015 at 7:04
  • No, pointers cannot be used as arrays. The subscript syntax only works for pointers. In fact, it's syntactic sugar for pointer arithmetic followed by defererencing. Now arrays decay into pointers in a lot of cases (this particular case being one), so they also appear to work with that syntax. The "real" meaning of a[index], if a is an array, is *(&a[0] + index). Commented Jan 29, 2015 at 11:33

3 Answers 3

4

t[i]=s[i] also worked which I guess is the syntactical sugar for it. Am I rt ?

Yes you are right s[i] = *(s+i);

Int the second code snippet you are moving your pointer t so now just do

*t = '\0';

instead of

t[i+1] = '\0'; /* Which is array out of bound access */
Sign up to request clarification or add additional context in comments.

3 Comments

Well guess what just happened I compiled both of the code and they just worked fine I shouldn't have posted this I guess, The compiler showed some strange behavior and just for information I am using an online compiler to run this thing
@Karan What you have is undefined behavior so you should get rid of it. It might work sometimes but please get the logic that you are accessing array out of bound which is UB. Don't think what you are doing is right if it works sometimes
Yes I get your point that my below code was not rt as setting t[i+1]='\0' will result in an garbage value in the string at the position t[i] rt ?
1
*(t+i)=*(s+i);  // t[i]=s[i] also worked  which I guess is the 
                   //syntactical sugar for it. Am I rt ? 

Indeed you are. pointer[index], in C, is equivalent to *(pointer + index).

However, it's not the same as this: *t++=*s++;. Here, you are changing your actual pointers. So, the new value of the pointer t will be t + i. That's why t[i + 1], in terms of the original value of t, becomes *(t + i + i + 1), which is definitely not what you want.

2 Comments

Well I didn't get your point while using *t++=*s++ yes I am moving the pointers and storing the value of memory location of s into t and where t initially points to starting address of newmess thus incrementing t will make it point to next location of the array newmess rt ? and secondly in my first code snippet I used *(t+i)=*(s+i) which is almost as same as *t++=*s++ though *(t+i) and *(s+i) didn't move the pointer actually but it is doing the same thing rt ?
@Karan: yes, if you leave out the fact that the second code increments the actual pointer, it's the same as the first one. However, the pointer being incremented is not something that can be just left out if you're using the pointer afterwards. I.e. the problem in the second code is in t[i+1]='\0';, which gives different results compared to the first code because you've increased t.
0

In the new code t[i+1] is (approximately) equivalent to (t+i)[i+1] (or t[i + i + 1]) in the old code.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.