I am having the hardest time figuring out how to pass a pointer as an argument. All of the examples I have come across are using predefined array sizes while my array is dynamic. The following code is a simple representation of what I am trying to do in a larger project.

There are 3 files (header.h, main.c, and Display.c). In main.c I am asking the user to input several values and then it will display what is in the dynamic memory by calling Display.c. However, it will not read the information correctly.

HEADER.H

void Display(int *);

MAIN.C

#include <stdio.h>
#include <stdlib.h>
#include "header.h"

main(void)
{
	int **matrices = NULL;
	int size = 6;
	int Index = 0;
	int i = 0;

	matrices = (int *) malloc(4 * sizeof(int));
	if(*matrices != NULL)
	{
		matrices [Index] = (int *) malloc( size * sizeof(int));
		
		if(matrices != NULL)
		{
			printf("Please enter several values: ");
	
			for(i = 0; i <= size; i++)
			{
				scanf("%d", &matrices[Index][i]); //Would like to read the information in array matrices[Index].
			}
			Display(&matrices[Index]);
		}
	}
return 0; //Successful Termination

}

DISPLAY.C

void Display(int *matrices)
{
int i = 0;
int Index = 0;
int size = 6;

for (i = 0; i <= size; i++)
{
	printf("%d", matrices[Index]);
}

}

Any help would be greatly appreciated!

Recommended Answers

All 6 Replies

I think the bug is in line 12.

matrices = (int *) malloc(4 * sizeof(int));

'matrices' is a pointer to a pointer and you have typecasted whatever malloc returns to an int *. Try typecasting it into int **.

matrices = (int **) malloc(4 * sizeof(int));

That was spot on Anirudh Rb! I now am having a problem entering values into the dynamic memory. Line 17-20 is a for loop to increment the variable 'i' so a series of different values will be stored in the dynamic memory. However, it only reads in the first value and ignores the rest. Below is an updated version of my code.

main(void)
{
	int **matrices = NULL;
	int size = 6;
	int Index = 0;
	int i = 0;

	matrices = (int **) malloc(4 * sizeof(int));
	if(*matrices != NULL)
	{
		matrices [Index] = (int *) malloc( size * sizeof(int));
		
		if(matrices != NULL)
		{
			printf("Please enter several values: ");
	
			for(i = 0; i <= size; i++)
			{
				scanf("%d", &matrices[Index][i]);
			}
			Display(*matrices);
		}
	}
return 0; //Successful Termination
}
void Display(int *matrices)
{
int i = 0;
int Index = 0;
int size = 6;

for (i = 0; i <= size; i++)
{
	printf("%d", matrices[Index]);
}

}

All of the necessary pre-processor directives are included. Thanks for all your help!

I now am having a problem entering values into the dynamic memory.

It's not a good idea to ask for favors for every single problem you face. You should learn how to debug your own code at least.

Line 17-20 is a for loop to increment the variable 'i' so a series of different values will be stored in the dynamic memory. However, it only reads in the first value and ignores the rest.

The problem is in your 'Display' function (in line 9 of your second code-see carefully) and that's your homework.

You should learn how to debug your own code at least.

Firstly, I do debug my own code and pretty extensively. I am having trouble understanding the concepts of pointers and then applying them to my project. It is not the debugging that is an issue for me.

...that's your homework

Next, this is not my homework. My homework is much more extensive than this. Since I was having trouble debugging the issue in my larger project my professor suggested that I make a smaller one that focuses on certain aspects of the project that was not working. That issue is pointers and nothing more than that.

Now, you have suggested that the problem is in line 9 of my Display function. As someone who has done several projects I looked at both the printf statement and the section of the code that inputs data into memory. Currently, there are no errors that occurs.

I'm thinking that your solution was the following for line 9.

printf("%d", matrices[Index][i])

so the for loop can read the values of matrices [Index] at all of the positions . However, by inserting it creates the following error and will not compile.

"error C2109: subscript requires array or pointer type"

Once the subscript is removed everything falls back into place and works but only shows the first value. So through my debugging which I had done prior to my previous post shows that the error is not in line 9 of the display function and if it is I cannot figure out the solution.

...that's your homework

I'm sorry that I sounded like your professor up there, but my only intention was to help you find the bug, and it was simple so I thought that you were able to find that on your own.

I'm thinking that your solution was the following for line 9.

printf("%d", matrices[Index][i])

No, it was not.
It was this:

printf("%d",matrices[i]);

Because, the pointer "matrices" you're passing to the function "Display" is equivalent to "matrices[index]" for which you dynamically allocated memory in your 'main'

matrices [Index] = (int *) malloc( size * sizeof(int));

And you only got the first member because 'Index' in your 'Display' function always remains zero.
So you can also do like this:

for (Index = 0; Index <= size; Index++)
{
    printf("%d", matrices[Index]);
}

And also I can't believe that someone who has done several projects is still having trouble understanding the concepts of pointers and then applying them?

Thank you! Your explanation was very helpful!

someone who has done several projects is still having trouble understanding the concepts of pointers and then applying them?

We were just given this project dealing with pointers. So I am new to pointers and its syntax and purposes. I was referring to debugging my own code and trying to figure out the errors on my own.

I never intend to get someone to do my work as I enjoy the challenge. I come here to seek help and hopefully some point in the future help someone else who might be confused like I was.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.