Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
C Programming
Newbie: Array of pointers to strings questions.
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
[QUOTE="Longfellow, post: 2413646"] <snip> Sorry about the example, which was flawed in more ways than I saw. It's rather obvious that I don't really understand this stuff, but I'm learning ;) Here's the corrected version with the changes you specify: #include <stdio.h> #include <stdlib.h> #include <string.h> const char *line1 = "This is line one."; const char *line2 = "This is line two."; const char *line3 = "This is line three."; const char *line4 = "This is line four."; char *array[4]; void printit(); int main(void) { int i; printf("\nThis is the individual print-outs:\n"); array[0] = malloc(sizeof strlen(line1)+1); strcpy(array[0], line1); printf("array[0] is: %s\n", array[0]); array[1] = malloc(sizeof strlen(line2)+1); strcpy(array[1], line2); printf("array[1] is: %s\n", array[1]); array[2] = malloc(sizeof strlen(line3)+1); strcpy(array[2], line3); printf("array[2] is: %s\n", array[2]); array[3] = malloc(sizeof strlen(line4)+1); strcpy(array[3], line4); printf("array[3] is: %s\n", array[3]); printf("\nThis is the main body print loop:\n"); for (i=0; i<4; ++i) printf("array[%d] is %s\n", i, array[i]); printit(); for (i=0; i<4; ++i) free(array[i]); return 0; } void printit() { int i; printf("\nThis is the 'printit()' function print loop:\n"); for(i=0; i<4; ++i) printf("array[%d] is: %s\n", i, array[i]); } This is the result: This is the individual print-outs: array[0] is: This is line one. array[1] is: This is line two. array[2] is: This is line three. array[3] is: This is line four. This is the main body print loop: array[0] is This is line array[1] is This is line array[2] is This is line array[3] is This is line four. This is the 'printit()' function print loop: array[0] is: This is line array[1] is: This is line array[2] is: This is line array[3] is: This is line four. What am I missing? Thanks, Longfellow[/i][/i][/i] [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
C Programming
Newbie: Array of pointers to strings questions.
Top