strTest.c

strTest.c

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

int main(void)
{
    char* s = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

    printf("*(s+2) = %c\n", *(s+2));
    s = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

    /*ERROR printf("**s = %c\n", **s);
    s = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";*/

    printf("c)UNDEFINED *(s+*s) = %c\n", *(s+*s));
    s = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

    printf("d) s[6] = %c\n", s[6]);
    s = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

    printf("e) (&(s[2]))[-1] = %c\n", (&(s[2]))[-1]);
    s = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

    printf("f) (&(s[-1]))[2] = %c\n", (&(s[-1]))[2]);
    s = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

    printf("g) *(s++) = %c\n", *(s++));
    s = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

    printf("h) *(++s) = %c\n", *(++s));
    s = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

    printf("i) s[strlen(s)-1] = %c\n", s[strlen(s)-1]);
    s = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

    printf("j) s[strlen(s + 1)] = %c\n", s[strlen(s + 1)]);
    s = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

    printf("k)UNDEFINED s[strlen(s - 1)] = %c\n", s[strlen(s - 1)]);
    s = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    
    printf("l) s[strlen(s + 1)-1] = %c\n", s[strlen(s + 1)-1]);
    s = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

    printf("m) *(s--) = %c\n", *(s--));
    s = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

    printf("n)UNDEFINED *(--s) = %c\n", *(--s));
    s = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    
    printf("o) s[*(s+6)-s[4]] = %c\n", s[*(s+6)-s[4]]);
    s = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

    printf("p)UNDEFINED s[*(s+6)-4] = %c\n", s[*(s+6)-4]);
    s = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

    printf("q) s[*(s+4)-*(s+3)] = %c\n", s[*(s+4)-*(s+3)]);
    s = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

    printf("r)NULL TERMINATOR *(s+strlen(s)) = %c\n", *(s+strlen(s)));
    s = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

    printf("s) s[*(s+strlen(s))] = %c\n", s[*(s+strlen(s))]);
    s = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

    return 0;
}

OUTPUT

linux05:/home/users/std10093>gcc -Wall px.c -o px
px.c: In function 'main':
px.c:62: warning: array subscript has type 'char'
linux05:/home/users/std10093>px
*(s+2) = C
c)UNDEFINED *(s+*s) =
d) s[6] = G
e) (&(s[2]))[-1] = B
f) (&(s[-1]))[2] = B
g) *(s++) = A
h) *(++s) = B
i) s[strlen(s)-1] = Z
j) s[strlen(s + 1)] = Z
k)UNDEFINED s[strlen(s - 1)] = A
l) s[strlen(s + 1)-1] = Y
m) *(s--) = A
n)UNDEFINED *(--s) =
o) s[*(s+6)-s[4]] = C
p)UNDEFINED s[*(s+6)-4] = )
q) s[*(s+4)-*(s+3)] = B
r)NULL TERMINATOR *(s+strlen(s)) =
s) s[*(s+strlen(s))] = A
linux05:/home/users/std10093>

This code comes from USI.

Have questions about this code? Comments? Did you find a bug? Let me know!😀
Page created by G. (George) Samaras (DIT)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s