Jump to content
  • Advertisement
Sign in to follow this  
gp343

C Pointers Problems

This topic is 3658 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 there, I have the below code which is driving me nuts!! Basically, i'm trying to write a function that will populate a list that is created using malloc. Then I want to later iterate over that list to read all of its values. So far, I've a function called - void PopulateListFromControl(TCHAR** List, int ListSize);. The problem is that I "think" that this function is populating the list, however, whenever I try to iterate over the list and print out its values, I get an Access Violation error. I've tried passing in "ListOfItems" as a reference to the PopulateListFromControl function, but this again brought up more errors. Below is my full source code, can somebody please help?

#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <tchar.h>

#define MAX_LIST_SIZE 100

TCHAR **ListOfItems;

void PopulateListFromControl(TCHAR** List, int ListSize)
{
	int i;
	for(i = 0; i < ListSize; i++)
	{
		List = i; //Should this line be *List = i; instead?
	}
}


int main()
{
	int n;
	
	ListOfItems = (TCHAR**)malloc(MAX_LIST_SIZE * sizeof(TCHAR));
	memset(ListOfItems, _T(""), MAX_LIST_SIZE);
	PopulateListFromControl(ListOfItems, MAX_LIST_SIZE);
	
	for(n = 0; n < MAX_LIST_SIZE; n++)
		printf("Item = %s\n", ListOfItems[n]);
		
	free(ListOfItems);
	
	getch();
	return 0;
}


Thanks, Jr.

Share this post


Link to post
Share on other sites
Advertisement
malloc does not give you a TCHAR** here, but rather a TCHAR*. If you want an array of TCHAR arrays, you'll need to do things a little differently (also, naming an array List is confusing).


int i=0;
TCHAR **List = malloc(MAX_LIST_SIZE * sizeof(TCHAR*));
for(i; i < MAX_LIST_SIZE; ++i)
{
List = malloc(MAX_STRING_SIZE * sizeof(TCHAR));
}

Or something like that. My C is rusty.

What are you trying to do in PopulateListFromControl? Why are you assigning an int to a TCHAR*?

Share this post


Link to post
Share on other sites
Hi Driv3MeFar,

I'm currently trying to populate my array/list with some numbers from the UI. Nevertheless, I've tried the above code with char* and it is still failing.

Regards,
Jr

Share this post


Link to post
Share on other sites
Well, what do you want this array to hold? C strings or numbers? Or strings representing numbers?

The type of List is TCHAR*, a pointer to a character (presumably the first character of a null-terminated string, here). You're currently assigning that to an integer in the range [0,ListSize). This doesn't work. For example, after PopulateListFromControl, List[0] will be 0, a NULL pointer. You are then trying to print this as a string. That's bad mojo.

If you want your strings to hold numbers, you have to convert the numbers to strings, with a function like itoa.

Quote:
Original post by Hnefi
Shouldn't you use calloc() if you want indexable memory?


Probably.

Share this post


Link to post
Share on other sites
Quote:

Original post by Driv3MeFar
Quote:

Original post by Hnefi
Shouldn't you use calloc() if you want indexable memory?




Probably.


Actually, no. All you need to index memory as an array of objects of type T is a T*. The job of malloc and calloc is to initialize the pointer so that it points to newly allocated memory. The only difference between malloc and calloc is that the latter initializes the allocated memory memory to zero and accepts two parameters (no of elements, size of element), instead of one (byte count).

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!