Search code examples
cstringpointersstrlen

Identify the size of the address assigned to the pointer


In my code below:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZ 256   //arbitrary value

typedef char text_t;
text_t text[SIZ];


text_t *create_text()
{
    text_t *c;
    strcpy(text,"");
    c= text;
    return c;
}


int main()
{

text_t *point_to_text;

point_to_text=create_text();

printf("length of the returned pointer")

    return 0;

}

I want to assign point_to_text a string of length zero. I used strcpy(text,""); to copy an empty string. How can I show that length of the text is zero in this case. Also, if I use strcpy(text,"abc"); , how can I show the length is 3 in this case?


Solution

  • You can use

      length =  strlen(point_to_text);
    

    String Literals in C means a ASCII character codes stored at consecutive memory location terminated by null char(\0).

    strlen() expects the input to be a pointer to a constant char string terminated by null char(0 or '\0'). The function returns length of the string excluding null char.