c - getting the first value of a null initialized array of strings -


i trying first value of null initialed array of string.i have tried every method can think of no luck. either warning, errors, or segmentation fault. whats correct way print null value , use in if statement? once figured out loop @ end printing strings not equal null.

#include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h>  int main(int argc, char** argv)  {     char *strings_line_tokens[503] = {0};     int = 0;      strings_line_tokens[0] = malloc(strlen("cookies")+1);     strcpy(strings_line_tokens[0], "cookies");      strings_line_tokens[2] = malloc(strlen("foobar")+1);     strcpy(strings_line_tokens[2], "foobar");      printf("strings_line_tokens[]: %s\n",strings_line_tokens[0]);     printf("-------------------\n");      printf("strings_line_tokens[]: %c %d \n",(char)strings_line_tokens[1], (int)strings_line_tokens[1]);     printf("aaaaaaaaaaaaaaaa\n");      printf("strings_line_tokens[]: %c %d \n",strings_line_tokens[1], strings_line_tokens[1]);     printf("bbbbbbbbbbbbbbbb\n");      printf("strings_line_tokens[]: %c %d \n",(char)strings_line_tokens[1][0], (int)strings_line_tokens[1][0]);     printf("ccccccccccccccccccc\n");      printf("strings_line_tokens[]: %c %d \n",strings_line_tokens[1][0], strings_line_tokens[1][0]);     printf("-------------------\n");      if(strings_line_tokens[1] == 0)         printf("you have null \n");      for(i = 0; < 3; i++)         if(strings_line_tokens[i] != 0)             printf("strings_line_tokens[]: %s\n",strings_line_tokens[i]);     return 0; } 

here current warnings i'm getting.

main.c:13: warning: cast pointer integer of different size main.c:13: warning: cast pointer integer of different size main.c:15: warning: format ‘%c’ expects type ‘int’, argument 2 has type ‘char *’ main.c:15: warning: format ‘%d’ expects type ‘int’, argument 3 has type ‘char *’ 

here update m.m asked for. klas lindbäck showed how ultimate goal loop working.

from past experience when using char array initialized null %c give garbage in printf() , %d give ascii value of null 0 in printf(). thought 1 of these give garbage , ascii value 0.

printf("strings_line_tokens[]: %c %d \n",(char)strings_line_tokens[1], (int)strings_line_tokens[1]); printf("aaaaaaaaaaaaaaaa\n");  printf("strings_line_tokens[]: %c %d \n",strings_line_tokens[1], strings_line_tokens[1]); printf("bbbbbbbbbbbbbbbb\n");  printf("strings_line_tokens[]: %c %d \n",(char)strings_line_tokens[1][0], (int)strings_line_tokens[1][0]); printf("ccccccccccccccccccc\n");  printf("strings_line_tokens[]: %c %d \n",strings_line_tokens[1][0], strings_line_tokens[1][0]); printf("-------------------\n"); 

output hoping 1 of them.

garbage 0 

the for loop working.

i removed trial-and-error code , kept for loop:

#include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h>  int main(int argc, char** argv) {     char *strings_line_tokens[503] = {0};     int = 0;      strings_line_tokens[0] = malloc(strlen("cookies")+1);     strcpy(strings_line_tokens[0], "cookies");      strings_line_tokens[2] = malloc(strlen("foobar")+1);     strcpy(strings_line_tokens[2], "foobar");      for(i = 0; < 3; i++)         if(strings_line_tokens[i] != 0)             printf("strings_line_tokens[%d]: %s\n", i,strings_line_tokens[i]);     return 0; } 

output:

strings_line_tokens[0]: cookies strings_line_tokens[2]: foobar 

there no right way print null value. add else clause printed wanted, example string null:

for(i = 0; < 3; i++)     printf("strings_line_tokens[%d]: %s\n", i, strings_line_tokens[i] == 0 ? "null": strings_line_tokens[i]); 

or (if prefer regular if on ternary if):

for(i = 0; < 3; i++)     if (strings_line_tokens[i] == 0) {         printf("strings_line_tokens[%d]: %s\n", i, "null");     } else {         printf("strings_line_tokens[%d]: %s\n", i, strings_line_tokens[i]);     } 

new output:

strings_line_tokens[0]: cookies strings_line_tokens[1]: null strings_line_tokens[2]: foobar 

Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -