sanjitguin
sanjitguin

Reputation: 45

pointer in c program

program in c language

void main()      
{
 char *a,*b;    
 a[0]='s';    
 a[1]='a';    
 a[2]='n';    
 a[3]='j';    
 a[4]='i';   
 a[5]='t';   
 printf("length of a %d/n", strlen(a));  
 b[0]='s';  
 b[1]='a';   
 b[2]='n';   
 b[3]='j';   
 b[4]='i';  
 b[5]='t';       
 b[6]='g';      
 printf("length of b %d\n", strlen(b));  
}

here the output is :

length of a 6    
length of b 12

Why and please explain it.

thanks in advance.

Upvotes: 0

Views: 145

Answers (6)

SAQIB SOHAIL BHATTI
SAQIB SOHAIL BHATTI

Reputation: 79

a and b are both char pointers. First of all, you didn't initialise them and secondly didn't terminate them with NULL.

Upvotes: 0

Simson
Simson

Reputation: 3559

Allocate some memory and terminate the strings then it will work better

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


    void main(){
            char *a=malloc(10);
            char *b=malloc(10);
            if(a){
                    a[0]='s';
                    a[1]='a';
                    a[2]='n';
                    a[3]='j';
                    a[4]='i';
                    a[5]='t';
                    a[6]=(char)0;   
                    printf("length of a %d\n", (int)strlen(a));
            }else{
                    printf("Failed to allocate 10 bytes\n" );
            }
            if(b){
                    b[0]='s';
                    b[1]='a';
                    b[2]='n';
                    b[3]='j';
                    b[4]='i';
                    b[5]='t';
                    b[6]='g';
                    b[7]=(char)0;   

                    printf("length of b %d\n", (int)strlen(b));
            }else{
                    printf("Failed to allocate 10 bytes\n" );
            }
            free(a);
            free(b);
    }

Upvotes: 1

unwind
unwind

Reputation: 400069

The indexing operator is de-referencing the pointers a and b, but you never initialized those pointers to point at valid memory. Writing to un-initialized memory triggers undefined behavior.

You are simply "lucky" (or unlucky, it depends on your viewpoint) that the program doesn't crash, that the pointer values are such that you succeed in writing at those locations.

Note that you never write the termination character ('\0') to either string, but still get the "right" value from strlen(); this implies that a and b both point at memory that happens to be full of zeros. More luck.

This is a very broken program; that it manages to run "successfully" is because it's behavior is undefined, and undefined clearly includes "working as the programmer intended".

Upvotes: 0

Can
Can

Reputation: 8581

When you declare any variable it comes with whatever it had in memory previously where your application is running, and since pointers are essentially numbers, whatever number it had referenced to some random memory address.

Then, when setting a[i], the compiler interprets that as you want to step sizeof(a) bytes forward, thus, a[i] is equal to the address (a + i*1) (1 because chars use one byte).

Finally, C-strings need to be NULL terminated (\0, also known as sentinel), and methods like strlen go over the length of the string until hitting the sentinel, most likely, your memory had a stray 0 somewhere that caused strlen to stop.

Upvotes: 1

Sadique
Sadique

Reputation: 22841

You are assigning to pointer (which contains garbage) without allocating memory. What you are noticing is Undefined Behavior. Also main should return an int. Also it does not make sense to try and find the length of an array of chars which are not nul terminated.

This is how you can go about:

Sample code

Upvotes: 1

tangrs
tangrs

Reputation: 9940

Undefined behavior. That's all.

You're using an uninitialized pointer. After that, all bets are off as to what will happen.

Of course, we can attempt to explain why your particular implementation acts in a certain way but it'd be quite pointless outside of novelty.

Upvotes: 0

Related Questions