c - strlen sometimes equal to sizeof for null-terminated strings -
i know strlen
counts number of characters until (and excluding) null character '\0'
(or 0
) , sizeof
gives amount of space needed store string including null character, confused output of code.
question:
i expect result of strlen
consistently 1 less result of sizeof
because strings null-terminated, seems case string of length 4 , 8, excluding '\0' (i.e. 3rd , 5th results below). suspect same reason rubbish being printed @ end of string 1st, 2nd, , 3rd results. explain behavior?
i read related question, don't think that's what's happening here: strlen - length of string increased 1.
what code does:
in main
, creates array of integers 0, 2, 4, 6, , 8. , each of lengths, calls on function make_and_print_msgs
to:
- create string of length + 1 (for null character), e.g. length of 4, string "aaaa\0" created
- print message letter-by-letter using
%c
inprintf
- print string using
%s
inprintf
- finds
strlen
of string - finds
sizeof
string
output:
i data_length[i] -------------------- 0 0 msg intended be: msg printed string: � strlen(msg): 1 sizeof(msg): 1 1 2 msg intended be: aa msg printed string: aas strlen(msg): 3 sizeof(msg): 3 2 4 msg intended be: aaaa msg printed string: aaaa strlen(msg): 4 sizeof(msg): 5 3 6 msg intended be: aaaaaa msg printed string: aaaaaai strlen(msg): 7 sizeof(msg): 7 4 8 msg intended be: aaaaaaaa msg printed string: aaaaaaaa strlen(msg): 8 sizeof(msg): 9
code:
(sorry code bit long, that's why explained above. comments in code references python numpy functions.)
#include <stdio.h> #include <math.h> /* needed ceil */ #include <string.h> /* needed strlen */ void make_linspace(int a[], double start, double stop, int num) { /* fills array a[] (in place) linearly spaced values np.linspace in numpy (python) */ double spacing = (stop-start)/(num-1); int i; (i=0; i<num; i++){ a[i] = start + i*spacing; } } void make_and_print_msgs(int n_proc, int msglength) { /* create string called msg of length msglength + 1 (for null character '\0') */ char msg[msglength+1]; int i; printf("msg intended be: "); (i=0; i<msglength; i++) { msg[i] = 'a'; printf("%c", msg[i]); } msg[i+1] = '\0'; /* print message screen string , fine strlen(msg) , sizeof(msg) */ printf("\n"); printf("msg printed string: %s\n", msg); printf("strlen(msg): %d\n", strlen(msg)); printf("sizeof(msg): %d\n\n", sizeof(msg)); } void main(int argc, char *argv[]) { int n_proc = 2; /* create array containing lengths of strings printed (in case, data_length should {0, 2, 4, 6, 8} */ int start = 0; int stop_range = 10; /* stop value if using range() */ int step = 2; /* spacing between integers in output of range() */ int stop = stop_range - step; /* stop value if using linspace() */ int npoints = (int) ceil( ((double)stop_range - (double)start) / (double)step ); /* number of elements in list produced range(start, stop_range, step) */ int data_length[npoints]; /* 1d array of string lengths (# of non-null chars in each str) */ make_linspace(data_length, start, stop, npoints); int i; /* each length, call on make_and_print_msgs make string of length (plus '\0') , print stdout */ printf(" data_length[i]\n--------------------\n"); (i=0; i<npoints; i++) { printf("%4d %7d\n", i, data_length[i]); make_and_print_msgs(n_proc, data_length[i]); } }
change : msg[i+1] = '\0';
msg[i] = '\0';
you not need increment i
incremented previous for loop
.
working ideone link: http://ideone.com/gjo1q1
Comments
Post a Comment