2

I'm a fairly new in C programming and a new member of stackoverflow community, although I'm pretty familiar to tex.stackexchange, and I'm having a syntax problem.

I was wondering how could I allocate one dimension of an array dynamically while having the other allocated statically. I could find answer in this question. I just need to declare a pointer to an array, and not an array of pointers, as answers tell so. Problem is I can't think of how to call a function of this "mixedly allocated" array (do I have any proper way to call it?) as a parameter, since my variable is declared as follows:

char (*strings)[maxlen];

Where maxlen is a global variable informing each string's length. Then I dynamically allocate it, so I have N strings, each of which with a length maxlen:

strings = malloc(N*sizeof(char));

So I would like to call a function of this array of strings, but I can't think of a way to declare it. My first shot was to silly give a try at

void func(char **, int);

and then call

func(strings,N);

But it wouldn't work, because I don't have a char ** argument, but a char (*)[100] (my maxlen is 100). So I get the following error:

expected ‘char **’ but argument is of type ‘char (*)[100]’

of course.

Well, my problem could probably be solved if I choose to allocate both dimensions dynamically, but that's not my intention.

How could I declare a function where my variable would go through it without a problem?

Thanks in advance.

EDIT: maxlen is a macro, known at compile time.

My minimal example code (not tested yet):

 #include<stdio.h>
 #include<string.h>
 #include<stdlib.h>

 #define maxlen 100


 void func(char (?), int N);

 int main()
 {

    char (*strings)[maxlen];
    int i, N;

    scanf("%d", &N);
    getchar();
    strings = malloc(N*sizeof(char));
    for(i=0;i<N;i++)
    {
       fgets(strings[i],sizeof(strings[i]),stdin);
    }

    func(strings,N);

    return 0;

 }

 void func(char (?), int N) ...
Community
  • 1
  • 1
a-sf-d
  • 23
  • 3
  • `char (*strings)[maxlen];` is an array named `strings` that has `maxlen` elements, of which each is a pointer to a function that returns a `char` It's probably not, what you are looking for. Try `char *strings[maxlen]; strings = malloc(N*sizeof(char));`. – Miroslav Cetojevic Oct 08 '16 at 23:13
  • `error: assignment to expression with array type` `strings = malloc(N*sizeof(char));` By declaring `char *strings[maxlen];`, I have `strings` as an array sized `maxlen` of pointer to char, while `char (*strings)[maxlen];` gives `strings` as a pointer to an array sized `maxlen` of char. – a-sf-d Oct 08 '16 at 23:28
  • It's easiest, if you do this: `char **string = malloc(N*sizeof(char*));`. This is equivalent to `char *string[N]`. Now you can pass it like this: `func(string, N);`. – Miroslav Cetojevic Oct 09 '16 at 00:32
  • is `maxlen` known at compile-time? – M.M Oct 09 '16 at 01:08
  • It would improve the question to show the real code you used to allocate and fill the array – M.M Oct 09 '16 at 01:17
  • In the minimal example `maxlen` is a macro , not a global variable (it makes a difference) – M.M Oct 09 '16 at 02:26
  • Apologies for my mistake. I will correct it. I really appreciate your efforts, thanks! – a-sf-d Oct 09 '16 at 02:30

3 Answers3

2

Your question isn't clear whether maxlen is known at compile-time or not. The following code works in both cases. If it is known at compile-time then you don't need to pass it as parameter to func.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

void func( int n_strings, int maxlen, char (*ps)[maxlen] )
{
    for (int i = 0; i < n_strings; ++i)
        printf("%s\n", ps[i]);
}

int main()
{
// automatic allocation
    char arr[4][7] = { "Red", "Blue", "Green", "Yellow" };
    func(4, 7, arr);

// dynamic allocation
    int maxlen = 7;
    char (*arr2)[maxlen] = calloc(4, maxlen);
    strcpy(arr2[0], "Red");
    strcpy(arr2[1], "Blue");
    strcpy(arr2[2], "Green");
    strcpy(arr2[3], "Yellow");
    func(4, 7, arr2);
}
M.M
  • 138,810
  • 21
  • 208
  • 365
0

You need to change your arguments for malloc.

char (*strings)[maxlen];

strings = (char(*)[maxlen])malloc(N*sizeof(char[maxlen]));

( char(*)[maxlen] ) before malloc tells what the type of your variable is

N*sizeof(char[maxlen]) inside malloc because sizeof(char[maxlen]) is the size you need for one string

How to:

define func: void func(char (*string)[maxlen]){.....}

Get addres of a string inside array strings : strings+i

Acces strings : strings[i] where 0 <= i < N

Have in mind :

&strings[i] is the same as strings+i --> is an address of a string

strings[i] is the same as *(strings+i)--> is a concrete string

JPX
  • 93
  • 1
  • 4
-2

try this:

void func(char *[100], int);