FAQ Database Discussion Community


strlen returning wrong value

c,data-structures,strlen
I'm building a text editor using doubly linked lists. These are my structs: #define N 4 typedef struct node { char data[N]; int size; struct node* next; struct node* prev; }node; typedef struct text { struct node* head; struct node* tail; int count; int size; }text; This is the piece...

segmentation fault while calling strlen with a previously allocated pointer

c,pointers,char,segmentation-fault,strlen
Up until now I have had some code that worked perfectly regarding a variable called wfiles. wfiles is initialized within my main file: char* wfiles = ""; Which as far as I can tell C has no complaints. Next the wfiles variable is allocated in a switch statement: switch (c)...

C: Count letters until specific entry inserted

c,strcmp,strlen
I'm a bit rusty on C. I'm in a school assignment that is asking me to make a program that gets user input, following that print the output. If the character count is higher than 50, to not print anything out and reprompt them. Quit should not print a count...

strlen sometimes equal to sizeof for null-terminated strings

c,string,null,sizeof,strlen
I know that strlen counts the number of characters up until (and excluding) the null character '\0' (or 0) and that sizeof gives the amount of space needed to store the string including the null character, but am confused with the output of my code. Question: I expect the result...

Input returns the correct string but doesn't run the function

c,string,malloc,getline,strlen
I have the following code: bool get_command(char *cmd){ char *cm = (char*)malloc(strlen(cmd)); strcpy(cm,cmd); const char *tok = strtok(cm," "); if(!tok) return false; if(!strcmp(tok,"MULTIPLY")) { printf("WORKING!\n"); return true } .... int main(void){ while(1){ char *input = NULL; char buf[MAX_LINE_LENGTH] = {0}; int read; size_t len; read = getline(&input, &len, stdin); if...

Does strlen() always correctly report the number of char's in a pointer initialized string?

c,strlen
As long as I use the char and not some wchar_t type to declare a string will strlen() correctly report the number of chars in the string or are there some very specific cases I need to be aware of? Here is an example: char *something = "Report all my...

why is -1>strlen(t) true in C? [duplicate]

c,strlen
This question already has an answer here: void main() { if(sizeof(int) > -1) printf(“true”); else printf(“false”); ; [duplicate] 3 answers Why sizeof(int) is not greater than -1? [duplicate] 2 answers Working on this little piece of code in VS2013, but for some reason it doesn't print.it seems that -1>strlen(str)...

What is the purpose of strlen($query)-2;

php,substr,strlen
I got this code from google and it fulfills my requirement, but I don't understand the meaning of this line: substr($query,0,strlen($query)-2) Could somebody explain it to me? <?php function insert($tablename, $parameter_order, $values) { $query = "insert into $tablename ("; foreach($parameter_order as $po) { $query .= $po.', '; } $query =...

Need to Evaluate strlen of an XML

php,xml,rss,strlen
Hopefully this is simple. In PHP I am trying to scrape some RSSfeeds. Sometimes, the RSS feed is not properly updated/populated, yet it is NOT an empty file. I have the following code: $xml = simplexml_load_file("feed-url-here"); if ($xml != "") { if (strlen($xml) > 600) { perform some actions here;...

Remove array element based on its character length

php,arrays,unset,strlen,array-unset
I have an array in PHP as: $array = array("Linda","Chadwick","Bari","Angela","Marco"); Therefore, $array[0]="Linda" $array[1]="Chadwick" $array[2]="Bari" $array[3]="Angela" $array[4]="Marco" I want to remove the names having string length <=4. So that, the keys are adjusted. $array[0]="Linda" $array[1]="Chadwick" $array[2]="Angela" $array[3]="Marco" ...