2d array, using calloc in C

I'm trying to create a 2D array of chars to storage lines of chars. For Example:

lines[0]="Hello"; lines[1]="Your Back"; lines[2]="Bye";

Since lines has to be dynamically cause i don't know how many lines i need at first. Here is the code i have:

int i; char **lines= (char**) calloc(size, sizeof(char*)); for ( i = 0; i < size; i++ ){ lines[i] = (char*) calloc(200, sizeof(char)); } for ( i = 0; i < size; i++ ){ free(lines[i]); } free(lines);

I know that each line can't go over 200 chars. I keep getting errors like "error C2059: syntax error : 'for'" and such. Any ideas of what i did wrong?

-------------Problems Reply------------

No the code is not in a function.

You can't just put arbitrary statements outside of functions in C and C++. What you can do though is use a function to initialize the variable:

char** init_lines() {
char** ln = /* ... */;
// your allocations etc. here
return ln;

char** lines = init_lines();

You tagged the question with C++ -- why not use a std::vector<std::vector<char> > for this?

Looking at the compiler error, it looks like you're missing a semicolon before one of your for loops -- the code you posted seems to work perfectly fine here.

For a start, it's a waste of time doing that first calloc since you immediately initialise them with the first for loop.

Having said that, there's nothing at all wrong with the code you've shown.

Therefore, either your error lies elsewhere or that's not the code you've posted. I suggest you post the exact error message along with a cut-and-pasted copy of the offending line and ten lines either side of it for context. That will make our lives a lot easier in helping you out.

The errors:

syntax error : 'for' syntax error : missing ')' before ';'
syntax error : missing ';' before '<' missing type specifier - int assumed

as shown in one of your comments is usually caused by unbalanced parentheses. Check all your ( and ) characters to ensure they're equal in number, and in the right place. It's probably because you're missing a ) in the statement before the for but that's just an educated guess since the code you posted does not have that problem.

Here i'm opinion is different. May be useful. char pointer or char double is implicitly define during compilation. Hence it is not need to define explicitly and by doing so it show syntax error. Try char pointer without initializing by calloc and if you don't want garbage value initialize with NULL. It act like using calloc, you find nothing different.

Category:c# Views:0 Time:2010-04-19

Related post

  • How to dynamically allocate a struct and it's TCHAR member array with calloc? 2011-07-26

    I can setup a structure easy enough on the stack: struct sArray { TCHAR sName[512]; }; struct sArray sKeys[2048] = {0}; But that is using way too much of the stack space. Instead, I want to put it all on the heap, so I came up with this: struct sArra

  • How to declare the size of an array at runtime in C? 2009-03-15

    I basically want to the C of equivalent of this (well, just the part with the array, I don't need the class and string parsing and all that): public class Example { static int[] foo; public static void main(String[] args) { int size = Integer.parseIn

  • C / C++ How to copy a multidimensional char array without nested loops? 2010-02-09

    I'm looking for a smart way to copy a multidimensional char array to a new destination. I want to duplicate the char array because I want to edit the content without changing the source array. I could build nested loops to copy every char by hand but

  • calloc v/s malloc and time efficiency 2010-04-09

    I've read with interest the post C difference between malloc and calloc. I'm using malloc in my code and would like to know what difference I'll have using calloc instead. My present (pseudo)code with malloc: Scenario 1 int main() { allocate large ar

  • MPI_Scatter - sending columns of 2D array 2011-04-01

    I want to send 2D array's columns, each to separate process. I have now one whole 2d array and I am stuck with MPI_Scatter. How to send whole columns as a field? Thanks edit: I have array - float a[100][101] and I have tried to send array by: float s

  • MultiDimensional Array in Objective c 2011-08-29

    EveryBody.. i want to create one 8*8 dimensional array in objective c.. ( [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0

  • id array member instance under ARC 2011-09-05

    I want to write something like this: @interface Foo{ __strong id idArray[]; } @end But the compiler complains about it: Field has incomplete type '__strong id []'. How can I create an id array member instance under ARC? And how do I init that array?

  • 2d array in C. Fixed width but unknown height? 2011-09-29

    Is it possible to make an array with an unknown height but a set width? So that It can be something like: |----------8-----------| [0][0][0][0][0][0][0][0] | [1][1][1][1][1][1][1][1] | [2][2][2][2][2][2][2][2] | [3][3][3][3][3][3][3][3] \|/ So that i

  • 2 Dimensional array and Print out average of the Columns and Rows 2011-11-07

    Im still a beginner in C programming and I need a little help writing a code for my C programming class. The prompt is: Input for this program is a two-dimensional array of floating point data located in a file named textfile94. The input array will

  • MPI error: subscripted value is neither array nor pointer 2011-12-02

    This assignment requires me to develop a MPI program for the parallel Odd-Even sort and brings together three functions: MPI_Compare_exchange() for the P2P compare and exchange operation MPI_Sort() for the parallel odd-even sort operation MPI_Is_sort

  • How to initialize a dynamic int array elements to 0 in C 2012-01-07

    i created a dynamic array ,and i need to initialize all the members to 0 . How can this be done in C? int* array; array = (int*) malloc(n*sizeof(int)); --------------Solutions------------- In this case you would use calloc(): array = (int*) calloc(n,

  • calloc / malloc and read weird behavior? 2012-01-09

    I'm trying to keep a dynamically allocated array of strings that are read in using the read system call in c. Here is a small sample of what I am trying to do: #include <stdio.h> #include <stdlib.h> #include <unistd.h> void processI

  • Segmentation fault by writing in a 2D Array 2012-02-03

    I have a small memory access problem in my program and I do not find the error, maybe someone could help me. I have created a new type to store rgb color values. That type looks like: typedef struct pixel { unsigned char r; unsigned char g; unsigned

  • MPI, calloc and free: 2012-04-22

    I'm trying to learn C and MPI, this program calculates the sum of n floats. But I have an error: /home/xx/PRIMO/primo.exe: free(): invalid next size (fast): 0x000000000109bda0 /home/xx/PRIMO/primo.exe: free(): invalid next size (fast): 0x00000000024f

  • Passing 3-dimensional numpy array to C 2013-03-22

    I'm writing a C extension to my Python program for speed purposes, and running into some very strange behaviour trying to pass in a 3-dimensional numpy array. It works with a 2-dimensional array, but I'm sure I'm screwing something up with the pointe

  • How to MPI_Gatherv columns from processor, where each process may send different number of columns 2011-03-20

    Say 2 processes are participating. Process 0 (rank 0) has A = { a d b e c f } and process 1 (rank 1) has A = { g h i } I want both processors to send these columns to rank 0 so that rank 0 will have the following in say another 2D-array. B = { a d g

  • Code is working in Windows but not in Linux! Why? [Simple pointer problem] 2011-05-21

    This is a working code snippet of a transportation problem (Removed the actual function. Only input and output functions are here. And BTW, it's incorrect) # include <stdio.h> # include <stdlib.h> typedef struct transport { int cost; int

  • How to create in Objective-C? 2011-09-17

    At the end of the day, i'd like to do NSDictionary *nodeData = [[NSDictionary alloc] initWithObjects:numbers forKeys:characters]; where numbers is an NSArray of numbers and characters is NSArray of characters. I suspect i can always do NSMutableArray

  • MPI Seg. fault when using send/recv with Dynamic Allocation 2011-12-02

    I'm dealing with dynamic allocated matrices, and need to send/recv them from a process to another. I'm not sure if I'm doing it correctly, but sometimes, with small matrices, it works; but when I increase their dimensions, it gets this error: [caio-A

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.193 (s). 11 q(s)