You want:
for (i = 0; i < strlen(source); i++) {
sizeof gives you the size of the pointer, not the string. However, it would have worked if you had declared the pointer as an array:
char source[] = "This is an example.";
but if you pass the array to function, that too will decay to a pointer. For strings it’s best to always use strlen. And note what others have said about changing printf to use %c. And also, taking mmyers comments on efficiency into account, it would be better to move the call to strlen out of the loop:
int len = strlen(source); for (i = 0; i < len; i++) {
or rewrite the loop:
for (i = 0; source[i] != 0; i++) {
Related Posts:
- Iterative Fibonacci code in Java?
- How to use execvp()
- How to use execvp() to execute a command
- How does strtok() split the string into tokens in C?
- what is Segmentation fault (core dumped)? [duplicate]
- How to convert an int to string in C?
- How to print in C
- What is newline character — ‘\n’
- Why does ENOENT mean “No such file or directory”?
- What does the question mark character (‘?’) mean?
- makefile:4: *** missing separator. Stop
- C Linking Error: undefined reference to ‘main’
- Undefined reference to pthread_create
- What is Bit Masking?
- segmentation fault : 11
- What is the C version of RMI
- How to convert an int to string in C?
- Iterate through a HashMap [duplicate]
- How do I use extern to share variables between source files?
- Cannot assign requested address – possible causes?
- Invalid read of size 8 – Valgrind + C
- error: called object is not a function or function pointer
- Is there any format specifier of bool in C?
- how to open an mp3 file with c
- munmap_chunk(): invalid pointer
- No Symbol Table using GDB on Compiled Programs
- What is char ** in C?
- What is the difference between a static and const variable?
- What is a bus error? Is it different from a segmentation fault?
- unsigned int vs. size_t
- Get the current time in C
- error: expected primary-expression before ‘)’ token (C)
- Bitwise operation |= in C
- Going through a text file line by line in C
- What’s the equivalent of new/delete of C++ in C?
- What is activation record in the context of C and C++?
- Get text from user input using C
- How do I lowercase a string in C?
- Fastest way to check if a file exist using standard C++/C++11,14,17/C?
- warning: expression result unused
- Print singly linked list in reverse order
- Why is volatile needed in C?
- error: indirection requires pointer operand (‘int’ invalid)
- What is the difference between exit(0) and exit(1) in C?
- Thread 1: EXC_BAD_ACCESS (code=1, address=0x0) standard C memory issue
- How to print a char array in C through printf?
- How to clear all the elements of array in C?
- Expected declaration specifier error in function
- How to initialize array to 0 in C?
- 2D array vs array of arrays
- make: Nothing to be done for `all’
- strcmp not working
- Setting std=c99 flag in GCC
- error: ‘for’ loop initial declarations are only allowed in C99 mode [duplicate]
- c – warning: implicit declaration of function ‘printf’
- error: aggregate value used where an integer was expected
- Implementing shell in C and need help handling input/output redirection
- char pointers: invalid conversion from ‘char*’ to ‘char’?
- Compiling a .C file: Undefined symbols for architecture x86_64
- What’s the difference between nexti and stepi in gdb?
- Incorrect checksum for freed object on malloc
- Is there any simple way to benchmark Python script?
- ISO C90 forbids mixed declarations and code in C
- Implementation of multiple pipes in C
- What does the term “empty loop” refer to exactly in C and C++?
- Invalid type argument of -> C structs
- Is there any difference between 1U and 1 in C?
- What integer hash function are good that accepts an integer hash key?
- X86 assembly – Handling the IDIV instruction
- Pause screen at program completion in C
- How do you clear the console screen in C?
- Does C have a string type?
- Difference between fgets and fscanf?
- How can I find the number of elements in an array?
- Array type char[] is not assignable
- What is the job of autogen.sh when building a c++ package on Linux
- Lua – Number to string behaviour
- error: function returns address of local variable
- Parameter name omitted error?
- Character constant too long for its type
- Representing EOF in C code?
- Return a `struct` from a function in C
- C: scanf to array
- What are “prototypes” in a C program?
- Realloc Invalid Pointer in C
- What’s the difference between “mod” and “remainder”?
- undefined reference to `std::ios_base::Init::Init()’
- Converting an integer to binary in C
- previous declaration of ‘function’ was here in C [duplicate]
- What tools are there for functional programming in C?
- typedef fixed length array
- Invalid write of size 1
- How to solve static declaration follows non-static declaration in GCC C code?
- realloc(): invalid next size when reallocating to make space for strcat on char *
- gdb: No symbol “i” in current context
- Cross Platform C library for GUI Apps?
- Pre increment vs Post increment in array
- Whats the difference between UInt8 and uint8_t
- How to Compare 2 Character Arrays [duplicate]
- How to use redirection in C for file input