-3

Please solve this problem ....

#include<stdio.h>

main()
{
    char *name;
    int length;
    char *cptr=name;
    name="Delhi";
    printf("%s\n",name);
    while(*cptr != '\0')
    {
        printf("%c is stored at address %u\n",*cptr,cptr);
        cptr++;
    }
    length=cptr-name;
    printf("\n Length of the string = %d\n",length);
    return 0;
}

Screenshot

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Sudip Sen
  • 5
  • 3
  • 4
    Stackoverflow is not a solution factory. It is a question and answer forum. Please give more information than just code and please specify what you've done to attempt to solve the problem. Also, try to google information about pointers because that's where your problem lies. – Gab Feb 14 '17 at 04:41
  • 2
    `int x; int y = x; x = 5; // why doesn't y contain 5?` – user253751 Feb 14 '17 at 04:50
  • 3
    You don't set `cptr` to a good value; you set it to the indeterminate value in `name` before you set `name` to a known value. This does not lead to happiness, but does lead to obscure comments from other people illustrating but not explaining the problem. Whether that's all that's wrong is a separate question. Your code is not C99 or C11 because you've not specified the return type of `main()`. – Jonathan Leffler Feb 14 '17 at 05:15
  • Note [the correct format specifier to print pointer address](http://stackoverflow.com/questions/9053658/correct-format-specifier-to-print-pointer-address/9053835#9053835). – Jonathan Leffler Feb 14 '17 at 05:17

2 Answers2

4
  • main()

    The prototype does not conform to the standard; it needs a return type of int. So change it to:

    int main(void) 
    

    or

    int main(int argc, char* argv[])
    
  • printf("%c is stored at address %u\n",*cptr,cptr);

    %u is not the correct specifier for a pointer; you need to use %p instead:

    printf("%c is stored at address %p\n",*cptr,(void*)cptr);
    
  • length=cptr-name;

    length is of type int and probably your machine is 64 bit so the variable cannot hold the difference of addresses which causes a problem so better using size_t.

    size_t length;
    

    and for the last output change the adequate specifier :

    printf("\n Length of the string = %zd\n",length);
    
  • Concerning getting the length of the string using only pointers put char *cptr=name; after name="Delhi";


Your program should be:

#include <stdio.h>

main()
{
    char *name;
    size_t length;
    name="Delhi";
    char *cptr=name;
    printf("%s\n",name);
    while(*cptr != '\0')
    {
        printf("%c is stored at address %p\n",*cptr,(void *)cptr);
        cptr++;
    }
    length=cptr-name;
    printf("\n Length of the string = %zd\n",length);
    return 0;
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
0

How to calculate length of string:

#include <stdio.h>                                                             

int main(void)                                                                 
{                                                                              

    char *s = "Hello World";                                                     
    int lengthIndex = 0;                                                       
    while(s[lengthIndex] != '\0')                                              
    {
        lengthIndex+=1;
    }                                                                          
    printf("Length of %s: is %d.\n",s,lengthIndex);                            
    return 0;                                                                          
}
WizKiz
  • 152
  • 1
  • 11