1

C++

1.

#include<iostream>
#include<cstring>

int main()
{
    char s1[20]="bonapart",s2[]="boy",*k;

    std::cout<<"hey";

    k=strstr(s1,s2);
    std::cout<<k;
    
    std::cout<<"bye";
}

OUTPUT

Hey

so why program is crashing here

2.

#include<iostream>

int main()
{

    std::cout<<"hey";
    
    std::cout<<nullptr;
    
    std::cout<<"hey";
}

OUTPUT

[Error] ambiguous overload for 'operator<<' (operand types are 'std::ostream {aka std::basic_ostream<char>}' and 'std::nullptr_t')

why this error ?

C

1.

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

int main()
{
    char s1[20]="bonapart",s2[]="boy",*k;
    
    printf("Hey");
    k=strstr(s1,s2);
    printf("%s",k);
    
    printf("bye");
}

OUTPUT

Hey(null)bye

works fine here

2.

#include<stdio.h>

int main()
{
    printf("Hey");
    
    printf("%s",NULL);
    
    printf("bye");
}

OUTPUT

Hey(null)bye

works fine.

So why in C++ code is crashing at runtime due to null string though C works fine by just putting null for null string.

Abhishek Mane
  • 583
  • 5
  • 20

0 Answers0