• Advertisement
Sign in to follow this  

Passing 2d char array through functions in c

This topic is 820 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi! 
I tried to learn how to pass a 2D char array through a function and then printing every value as a string.

 

this is how my code looks like:

void printName(char *name, int *players);

int main()
{
	char name[8][10];
	int players;

	printf("Enter how many players there are: ");
	scanf_s("%d", &players, sizeof(stdin));
	getchar();

	for (int i = 0; i < players; i++)
	{
		printf("\nEnter name: ");
		scanf_s("%s", name[i], 10);
		getchar();
	}

	printName((char*)name, &players);
	getchar();

	return 0;
}

void printName(char *name, int *players)
{

	printf("\n%s", &name[0]);

	for (int i = 1; i < *players; i++)
	{
		printf("\n\n%s", &name[i]);
	}

}

How is it that the for loop cuts down the second name instead of going through the array and print all the names?
What should i do to print out all the names as strings?

Share this post


Link to post
Share on other sites
Advertisement

A 2d array is not an 1d array.

void printName(char *name, int *players)  // wrong, name is said to be a 1d array here
void printName(char name[8][10], int *players)  // name is a 2d array, the first size is optional (you can leave out "8")
printf("\n\n%s", &name[i][0]);  // this is how you access it, or simple  "name[i]" will work too

I don't know what scanf_s  does, but "sizeof(stdin)" looks wrong, as it's the size of the stdin type (unless your compiler does something non-standard).

 

 

Hope this helps,

Albert

Share this post


Link to post
Share on other sites

void printName(char** name, int players)
{
    for (int i = 0; i < players; ++i)
    {
        printf("\n\n%s", name[i]);
    }
}

Share this post


Link to post
Share on other sites


void printName(char** name, int players)
{
    for (int i = 0; i < players; ++i)
    {
        printf("\n\n%s", name[i]);
    }
}

Sure that works? name doesn't look like a pointer to me ( in name[8][10] are only characters, no pointers to characters )

 

My compiler also has some problems:

x.c: In function 'main':
x.c:23:12: warning: passing argument 1 of 'printName' from incompatible pointer type [-Wincompatible-pointer-types]
  printName(name, &players);
            ^
x.c:4:6: note: expected 'char **' but argument is of type 'char (*)[10]'
 void printName(char **name, int *players);
Edited by Alberth

Share this post


Link to post
Share on other sites
To explain the differences, a char[8][12] is 96 contiguous bytes, packed tightly with 8 12-byte strings. To iterate through each string, you advance 12 bytes within the array. A char*[8], however, is 8*sizeof(void*) bytes, with pointers that don't necessarily point within a contiguous array.

Share this post


Link to post
Share on other sites

Thank you Guys!
I managed to solve the problem by changing the declaration to name[][10] like you said Alberth!
 

And scanf_s is just like scanf, the difference is that with scanf_s you have to define the number of character you want to enter.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement