C - extract comma separated strings from file into array - segmantation fault - c
I need to read from a file different strings that are comma-separated and storage them into an array.
I have the following code, that I developed reading different questions online.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main (){
int N = 200; // Number of sequences
int L = 1000; // length of sequences
char Nseq[N][L];
FILE *myfile;
char *token;
const char s[2] = ",";
char line[300];
char* filename = "pathofile.txt";
int n = 0;
myfile = fopen(filename, "r");
if (myfile == NULL) {printf("could not open file %s", filename); exit(0);}
while (fgets(line, sizeof(line), myfile) != NULL){
token = strtok(line, s);
while (token != NULL){
strcpy(Nseq[n], token);
printf("%s\t%u\n", token, n);
token = strtok(NULL, s);
n++;
}
}
fclose(myfile);
for (int n=0; n<100; n++){
printf ("%s\t%u\n", Nseq[n], n);}
}
my file is the following (there are 200 sequences):
AAAGCCGCCAAAGUAGGCGG,AAAGCCGCCAAAGUAGGCGG,AAAGCCGCCAAAGUAGGCGG,AAAGCCGCCAAAGUAGGCGG,AAAGCCGCCAAAGUAGGCGG,AAAGCCGCCAAAGUAGGCGG,AAAGCCCGCCAAAGAAGGCGG,AAAGCCCGCCAAAGAAGGCGG,AAAGCCCGCCAAAGAAGGCGG,AAAGCCCGGCCAAAGAAGGCGG,AAAGCCCGCCAAAGUAGGCGG,AAAGCCCGCCAAAGUAGGCGG,AAAGCCCGCCAGAAGUAGGCGG,AAAGCCCGCCAAAGUAGGCGG,AAAGCCCGCCAAAGUAGGCGG,AAAGCACCGCCAAUGGGCGG,AAAGCACCGCCAAUAGGCGG,AAAGCACCGCCAAUAGGCGG,AUAGCACCGCCAAUAGGCGG,AUAGCACCGCCAAUAGGCGG,AUAGCACCGCCAGUAGGCGG,AUAGCACCGCCAAUAGGCGG,AAAGCACCGCCAAAUAAGGCGGG,AAAGCACCGCCAAAUAAGGCGGG,AAAGCACCGCCAAAUAGGCGGG,AAAGCACCGCCAAAUAAGGCGG,AAAGCACCGCCAAAUAAGGCGG,AAAGCACCGCCAAAUAAGGCGG,AAAGCACCGCCAAAUAAGGCG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UCGGCGG 105
CACUCCGGCCAAGUCGGCGG 106
CACUGCCGGCCAAGUCGGCGG 107
CACUGCCGGACCAAGUCGGCGG 108
CACUGCCGGCCAAGUCGGCGG 109
UCAAUUGCCGGCCAAGUCGGCGG 110
UCAAUUGCCGGCCAAGUCGGCGG 111
UUUAAGGCCGCACAUGCGGCCGUG 112
UUAAGGCCGGAAACAUUCGGCCGUG 113
UUAAGGCCGCACAUUCGGCCGGG 114
UUAAGGCCGCACAUUCGGCCGGG 115
UUAAGGCCGCACAUUCGGCCGGG 116
UUAAAAGGCCGACAUUGCGGCCGGG 117
UUAAAGGCCGACAUUGCGGCCGGG 118
UUAAGUCCGCACAUUCGGCCGGG 119
UUAAGGCCGCACAUUCGGCCGGG 120
UUAAGGCCGCACAUUCGGCCGGG 121
UUAAGGCCGCACAUUCGGCCGGG 122
UUAAGGCCGCACAUUCGGCCGGG 123
UUAAGGCCGCACAUCGGCCGGG 124
UAAGGCCGCACAUUCGGCCGGG 125
UAAGGCCGCACAUUCGGCCGGG 126
UAAGGCCGGC 127
ACAUUCGGCCGGG 128
UAAGGCCGCACAUUCGGCCGGG 129
UAAGGCCGCACAUUCGGCCGGG 130
UAAGGCCGCACAUUCGGCCGGG 131
UAAGGCCGCACAUUCGGCCGGG 132
UAAGGCCGCACAUUCGGCCGGG 133
UAAGGCCGCACAUGUCGGCCGGGU 134
UAAGGCCGCACAUUCGGCCGGG 135
UAAGGCCGCACAUUCGGCCGGG 136
UAGGCCGCAAGUCGGCCGGG 137
UAGGCCGCAAGCGGCCGGG 138
UAGGCCGCAAGCGGCCGGG 139
UAGGCCGCAAGCGGCCGGG 140
UAGGCCGCAAGUCGGCCG 141
GG 142
UAGGCCGCAAGUCGGCCGGG 143
UAGGCCGCAAGUCGGCCGGG 144
UAGGCCGCAAGUCGGCCGGG 145
GAUCGGCCGGCAGCCUCCCGGCGG 146
GAUCGGCCGGCAGCCUCCCGGCGG 147
GAUCGGCCGGCAGCCUCCCGGCGG 148
GAUCG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UUGCGUGCCGCCUUAAGGCG 191
GUGUUGCGUGCCGCCUUAAGGCG 192
UUGGUCCGCCUUACGGCGGG 193
UUGGUCCGCCUUACGGCGGG 194
UUGGUCCG 195
CCUUACGGCGGG 196
UUGGUCCGCCUUACGGCGGG 197
UUCGUCCGCCUUACGGCGGG 198
GUUGUAGCCCGCCUUCGGCGGG 199
GUUGUUGCCGCCUUACGGCGG 200
GUUGUUGCCGCCUUACGGCGG 201
GUUGUUGCCGCCUUACGGCGG 202
GUUGUUGCCGCCUGACGGCGG 203
GUUGUUGCCGCCUGACGGCGG 204
GUUGUUGCCGCCUGACGGCGG 205
GUUGUUGCCGCCUGACGGCGG 206
GUUGUUUGCCGCCUGACGGCGG 207
GUUCCUUGCCAGCCUUACGGCGG 208
Segmentation fault (core dumped)
I know I have to solve the new line issue, but I don't know why do I get the segmentation fault. Because it seems to work but I don't get to the end of the file. Any ideas of what is causing this? Thank you
Because of the (arbitrary) size of your read buffer, you are splitting some sequences, so that your program sees more than 200 of them, and thus your array to hold them is too small.
char Nseq[N][L]; // N = 200;
It means that Nseq can store at most 200 sequences. But in your code, the value of n (in while loop) ups to 208 at least. To avoid the problem, you can define N more than 208 (N = 300, for example), or you have to add one condition for n in while loop condition as below:
while (fgets(line, sizeof(line), myfile) && n < 200) {...}
If you want to read all text in the file, you can use double pointer: char **Nseq, then realloc for it after each iteration of while loop:
char **Nseq;
while (fgets(line, sizeof(line), myfile)){
token = strtok(line, s);
while (token != NULL){
Nseq = realloc(Nseq, sizeof(char *) * (n+1));
if(!Nseq) {return -1;}
Nseq[n] = malloc(strlen(token) + 1);
if(!Nseq[n]) {return -1;}
strcpy(Nseq[n], token);
printf("%s\t%u\n", token, n);
n++;
token = strtok(NULL, s);
}
}
Related
Why aren't the characters being appended to the end of the string?
My program takes a string input from the user, looks through the string, finds a dash and prints any letters in between the two letters beside the dash (exercise 3-3 in "The C Programing Language"). My program always either adds too many letters or not enough. Here is the code that I think is responsible for the bug: for (i=0; i<(strlen(stringin));i++, w++){ c = stringin[i]; /*finds a - and logs the letters before*/ if(c == '-'){ printf("found - "); logpre = stringin[i-1]; printf("%d ", logpre); logpost = stringin[i+1]; printf("%d\n", logpost); /*logs newly generlated letters to string*/ for(o=logpre+1; logpre <= logpost; w++, o++){stringout[w]=o;} } else{ /*logs unused letters to string*/ stringout[w]=c; printf("%d\t%d\n", c, w); } } print statements are there because who knows how to debug without them? didn't know what to try, but this is the current input/output: a-z 0-9 String Length- 7 97 0 found - 97 122 211 7005 212 7006 213 7007 214 7008 55255 7009 14155992 7010 -654311207 7011 218 7012 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 - Input = ����������������������������������������������� Output = abcdefghijklmnopqrstuvwxyz{|}\~�������������������������������������������������������������������������������������������������������������������������������� this is what I want it to look like: a-z 0-9 String Length- 3 97 0 found - 97 122 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 31 48 49 50 51 52 53 54 55 56 57 - Input = a-z 0-9 Output = abcdefghijklmnopqrstuvwxyz 0123456789
for (i=0; i<(strlen(stringin));i++, w++) w++ here is wrong. It should be only incremented when you actually add to the output string. You also do not terminate you output string. Do it much more simple way: { size_t outPos = 0; if(str && *str && buff) { for(size_t pos = str[1] == '-'; str[pos]; pos++) { if(str[pos] == '-' && str[pos + 1]) { for(char ch = str[pos - 1]; ch <= str[pos + 1]; ch++) buff[outPos++] = ch; pos++; } else buff[outPos++] = str[pos]; } buff[outPos++] = 0; } return buff; } int main(void) { char buf[500]; printf("\"%s\"\n", print("asa a-z hello 0-3 wrewertyrt 5-W", buf)); }
C program to print right angle triangle
I am trying to write a program in C using for loops which will output a pattern similar to this: 1 2 3 4 5 6 7 8 9 10 As you can see the pattern is a right angled triangle with each row with the same amount of numbers as the first number in each row. I am a beginner in C and chose to go about solving to practice my use of for loops and done this step by step so i chose to first find out how i can print the first column. Here is my code so far: #include <stdio.h> int main() { int n; int p = 1; printf("enter number of rows: "); scanf("%d", &n); for (int i= 1; i <= n; i += p++){ printf("%d\n", i); } return 0; } What i found was that the first number of each column was being increased by 1 when broken down for example (1 + 1 = 2, 2 + 2 = 4, 4 + 3 = 7) As you can see each number i have made bold makes up the first column. My problem is that when i ask the use to enter a value to make up a specific number of rows, such as for example 5, the for statement prints out only 4 rows as you can see do below: enter number of rows: 5 1 2 4 I am guessing this is due to the statement i <= n but how do i specify the amount of rows without disrupting the calculation of the first column?
Here you have a triangle printing program. int ndigits(unsigned long long x) { int ndg = 0; while(x) { ndg++; x /= 10; } return ndg; } void triangle(int rows) { int count = 1; int ndg = ndigits(rows * (rows + 1) /2); for(int row = 1; row <= rows; row++) { for(int column = 1; column <= row; column++) { printf("%0*d ", ndg, count++); } printf("\n"); } } int main(void) { triangle(30); } the function ndigits calculates number of decimal digits of the number. It is needed to print real triangle as all numbers have to have the same size (some will be padded by zeroes). Otherwise the trangle will not look like a triangle. the rows * (rows + 1) /2 formula calculates the result of the 1+2+3+4+5... showing the largest number in the triangle three row triangle will look: 1 2 3 4 5 6 ten rows triangle: 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 and 20 rows: 001 002 003 004 005 006 007 008 009 010 011 012 013 014 015 016 017 018 019 020 021 022 023 024 025 026 027 028 029 030 031 032 033 034 035 036 037 038 039 040 041 042 043 044 045 046 047 048 049 050 051 052 053 054 055 056 057 058 059 060 061 062 063 064 065 066 067 068 069 070 071 072 073 074 075 076 077 078 079 080 081 082 083 084 085 086 087 088 089 090 091 092 093 094 095 096 097 098 099 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 you can try it yourself here: https://godbolt.org/z/7EWWso
You can use This Simple Code: #include <stdio.h> int main(){ int count=0; for (int i = 01; i < 11; ++i) { for (int j = 01; j <=i; ++j) { count+=01; printf("%d ",count); } printf("\n"); } }
How to properly align triangular number patterns
In C, I need to print the number pattern in the below manner in right alignment, ie: when the double digit comes the upper single digit should adjust itself to right and so on. int main() { long int k = 1; int i, j, n; scanf("%d", &n); for (i = 1; i <= n; i++) { for (j = 1; j <= i; j++) { printf("%ld ", k); k++; } printf("\n"); } return 0; } required output: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 ` output I get: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 output required: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 output I get: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
You should calculate the length of the last outputted number and use the value as the field width in a printf call.. Here you are. #include <stdio.h> int main(void) { while ( 1 ) { printf( "Enter a non-negative number (0 - exit): " ); unsigned int n; if ( scanf( "%u", &n ) != 1 || n == 0 ) break; unsigned int upper = n * ( n + 1 ) / 2; int size = 0; do { ++size; } while ( upper /= 10 ); putchar( '\n' ); for ( unsigned int i = 0, value = 0; i < n; i++ ) { do { printf( "%*u ", size, ++value ); } while ( value != ( i + 1 ) * ( i + 2 ) / 2 ); putchar( '\n' ); } putchar( '\n' ); } return 0; } The program output is Enter a non-negative number (0 - exit): 10 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 Enter a non-negative number (0 - exit): 20 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 Enter a non-negative number (0 - exit): 0
I will not help you write a code because zero effort is shown, but I can give hints via a pseudo code. cin >> n; //desired number of rows for (int i = 0; i < n; i++){ //generate triangle numbers }//store the first n triangle numbers, given by n(n-1)/2 for (int i = 0; i < n; i++){ cout << i; if (i==any triangle number){ cout << "\n"; //note that an endline is printed only at triangle numbers } }
Here is a simple solution that handles end of lines correctly too: #include <stdio.h> int main(void) { int i, j, k, n, len; if (scanf("%d", &n) == 1 && n > 0) { /* compute the width of the last number */ //len = snprintf(NULL, 0, "%d", n * (n + 1) / 2); //char buf[48]; /* large enough for 128-bit integers */ //len = sprintf(buf, "%d", n * (n + 1) / 2); for (len = 1, k = n * (n + 1) / 2; k > 9; k /= 10) len++; for (i = k = 1; i <= n; i++) { for (j = 1; j < i; j++) { printf("%*d ", len, k++); } /* print the last number on the line without a trailing space */ printf("%*d\n", len, k++); } } return 0; }
Code didn't calculate the right time execution of function in C
I have searched for how to calculate the execution time of a program, and i have done exactly in describe of these topic Execution time of C program Calculating time of execution with time() function but my code didn't count the time execution and i don't know why. here are my code #include <stdio.h> #include <stdlib.h> #include <time.h> int main(int argc, char** argv) { time_t begin, end; begin = clock(); int c = 0; for (int i = 0; i < 255; i++) { printf("%d ",c++); } end = clock(); printf("\n"); double timeTaken = ((double) (end - begin)) / CLOCKS_PER_SEC; printf("time taken: %lf", timeTaken); return (EXIT_SUCCESS); } here are what it printed 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 time taken: 0.000000 I have checked many times for error, but it still print "time taken: 0.000000". Can you guys help me out? Thanks a lot.
Actually, you are iterating loop for a small number of times. Try iterating the loop for large number of times. Eg. iterate for 1e9 times. Then you will get noticeable time period. Modern processors are very fast, such that they have frequency upto 2.9 GHz or more, which mean that they can execute around 2 billion instructions, if available, within a second.
Chop up string into chunks of 512 bytes in c
Im trying to write a function that takes a string and chops it up into chunks of 512 bytes. It takes the first 512 bytes in the string, and stores it in chopped[0] and then the next 512 in chopped[1]...etc. It seems to be working when I print out chopped in the function, but when I return chopped[0] is giving me the whole string and not just the first 512. Any ideas whats going on here? char** chopString(){ char string[]="1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200"; int len=strlen(string)+1; int i,j,bytes,blocks; int blockSize=512; bytes=len*sizeof(char); blocks=(int)ceil((double)bytes/blockSize); //determine number of blocks rounded up char* chunk=malloc(blockSize+1); char* newChunk=malloc(blockSize+1); char** chopped=malloc(sizeof(char*)*(blocks+1)); //blockSize+1 for null character for(j=0; j<blocks; j++){ len=strlen(string)+1; //get length of string if(len<blockSize){ //if the string can fit in one block bytes=len; //the number of bytes to write is the length }else{ //if it doesn't fit in one block bytes=blockSize; //then bytes to write is one block } strcpy(chunk,string); //copy newString into chunk newChunk=chunk; //keep pointer to begining of chunk for(i=0; i<bytes; i++){ chunk++; } strcpy(string,chunk); //set new string to remaining portion *chunk='\0'; //set end of chunk to null chopped[j]=newChunk; //put chunk into array printf("Chopped[%d]: %s\n",j,chopped[j]); printf("length: %d",(int)strlen(chopped[j])); } chopped[j]=NULL; printf("\n"); return chopped; }
strcpy copies the entire string. try strncpy. note you will need to add a null terminator to the end of the string. http://www.cplusplus.com/reference/cstring/strncpy/
There are numerous problems with the code. Here are a few. each call to malloc needs to have the returned value checked (!= NULL) to assure the operation was successful this line: 'newChunk=chunk;' overwrites the pointer that was returned by malloc, resulting in a memory leak strcpy() pays no attention to the amount of available bytes in the destination (1st) parameter. so the number of bytes needs to be limited to the length of the first parameter-1 then have a NUL byte appended the easiest fix is to use strncpy() the right way to setup a char ** is: 4a) allocate the array char** myArray = malloc( count* sizeof(char*) ) if (NULL == myArray) //handle error and exit else // following to make cleanup on error easy memset(myArray, 0x00, count*sizeof(char*) ) 4b) allocate each character string for( int i=0; i<count; i++ ) myArray[i] = malloc( blockSize+1 ) //+1 allows for termination byte if( NULL == myArray[i] ) // cleanup and exit 4c) cleanup would be for( int i=0; i<count; i++ ) free(myArray[i]) free(myArray)