Hello!
I'm having some trouble understanding how arrays are passed.
From my understanding, my code here passes a pointer to the first element of the blah array, which is "var1=5".

My question then is how can I move to the next element of my blah array, "var2=234234" in my filevars() function?

#define length(a) (sizeof (a) / sizeof *(a))
void filevars(const char *blah);
void filevars ( const char *blah )
{
	int i;
	for(i=0;i<length(blah);i++){
		printf("%s", &blah[i]);
	}
}
main(){
	char *blah[] = {
    "var1=5",
    "var2=234234"
	};
	filevars(*blah);
}

Thanks so much!

Take a look at the following example:

#include <stdio.h>

void print_el(char **p, int el)
{
    printf("%s\n", p[el]);
}

int main(void)
{
    char *blah[] = {
    "string1",
    "string2"
    };

    print_el(blah, 1);
    print_el(blah, 0);

    return 0;
}

output is:

string2
string1

>how can I move to the next element of my blah array
Increase the pointer, e.g.: if p is a pointer then you'll have to write p++; to move it to the next element :)

Thank you! That explains alot.

Would it be silly/impossible to try to do something like this:

const char *ini[];
	int i;
	for(i=0;i<el;i++){
		ini[i]=blah[i]
		printf("%s\n", blah[i]);
	}

Some remarks:

  1. const char *ini[]; will cause a compiler error because the size of this array isn't known.
    (You can solve this by putting the number of elements between the brackets ('') or by initializing the array)
  2. ini[i]=blah[i] , missing semicolon (';').
  3. This line: ini[i]=blah[i]; doesn't make much sense if the next line is: printf("%s\n", [B]blah[i][/B]); Perhaps you meant printf("%s\n", [B]ini[i][/B]); ??

Oh right. I didn't actually try to compile that, I was just giving a general example of what I wanted to do.

This compiles for me, but gives me the warning:
"assignment from incompatible pointer type".

char *ini[el];
	size_t i;
	for(i=0;i<el;i++){
		ini[i] = &(*blah[i]);
		printf("%s\n", blah[i]);
	}

Thanks again!

My compiler didn't give any warning about this (even with all warning levels on). &(*blah[i]) is actually the same as blah[i] :)

This question has already been answered. Start a new discussion instead.