Need Assistance! C pointers

PoitNarf

Junior Member
Oct 21, 2001
12
0
0
:confused:
I can't seem to fully understand pointers in C. Suppose I declare some function,

void func(int **p)
{
int y = 1;
p = &y;
}

I assume I call this by,
int *x;
func(&x);

After func completes, I want my pointer variable x to point to the address of where 1 is stored. The program that I wrote that uses something like this doesn't do this though. I know the above code is incorrect, (**p NEEDS TO STAY THERE). Hoping somebody who knows how to use pointers can help. Thanks.
 

HigherGround

Golden Member
Jan 9, 2000
1,827
0
0
dereference p before assigning it the address of y.

void func(int **p)
{
int y = 1;
*p = &y;
}

That has the same effect as saying *(&x) = &y or simply x= &y.
 

PoitNarf

Junior Member
Oct 21, 2001
12
0
0
Can't believe I didn't think of that, thanks! I guess this is what happens when you got too much Dew in ya.

 

PoitNarf

Junior Member
Oct 21, 2001
12
0
0
:(
Tried doing what you said, and now my program won't compile. It gives a warning: assignment from incompatible pointer type. I'll paste some of my actual code, my example was probably bad. This is not my complete code, just the segments that are relevant.

typedef struct ele wlist;
struct ele
{
char *word;
int freq;
wlist *next;
};

typedef struct alpha alist;
struct alpha
{
char let;
wlist *words;
alist *link;
};

void updateList(alist *index, char *word)
{
int wordfreq;
wlist *temp = NULL;
wlist *position2 = NULL;
position2 = (wlist *) malloc(sizeof (wlist));
wordfreq = find(index, word, &position2);
temp = (wlist *) malloc(sizeof (wlist));
if (position2->word == NULL) //first on list
{
temp->word = word;
temp->freq = 1;
temp->next = NULL;
position2 = temp;
}
}

int find(alist *index, char *word, wlist **position)
{
//only line that uses **position in this function
*position = &(index->words);
}

I've tried several different ways of passing in the 3rd argument of find, one of them is able to compile, but it does not perform the assignment position2 = temp; correctly. Any ideas?
 

HigherGround

Golden Member
Jan 9, 2000
1,827
0
0
index->words is already of type wlist *, hence you assignment should either look like ...

*position = index->words; /* type (wlist*) */

or

position = &(index->words); /* type (wlist**) */

in your original question, the assignment involved type int, not int*, therefore we needed to reference it to get the address.
 

PoitNarf

Junior Member
Oct 21, 2001
12
0
0
This thing still has me hung up. :eek: When the program exits out of lookup(alphalist *index, char *word, wordlist **position), it does return a memory location, but not the one that I set it equal to inside of the lookup function. All the printfs are there for my debugging. An example of the incorrect location assignment from my debug printf statements:

Position for word blue: 136452
test: 136452
Position2: 136592

I know I must be doing one stupid little thing wrong here. Any help would be greatly appreciated! Thanks.

void incr_freq(alphalist *index, char *word)
{
int wordfreq;
wordlist *temp = NULL;
wordlist *position2;
position2 = (wordlist *) malloc(sizeof (wordlist));
wordfreq = lookup(index, word, &position2);
printf("Position2: %d\n", position2); //debug
if (wordfreq > 0)
position2->freq = wordfreq + 1;
else
temp = (wordlist *) malloc(sizeof (wordlist));
if (position2->word == NULL) //first on list
{
temp->word = word;
temp->freq = 1;
temp->next = NULL;
position2 = temp;

}
else if (position2->next != NULL) //middle of list
{
temp->word = word;
temp->freq = 1;
temp->next = position2->next;
position2->next = temp;
}
else if (position2->next == NULL) //last on list
{
temp->word = word;
temp->freq = 1;
temp->next = NULL;
position2->next = temp;
}
}

int lookup(alphalist *index, char *word, wordlist **position)
{
int wordfreq = 0;
int length = strlen(word);
char word2[length];
strcpy(word2, word);
printf("\nlookup:word = %s", word); //debug
printf("\nlookup:index->let = %c", index->let); //debug
printf("\nlookup:word2 = %s", word2); //debug
printf("\nlookup:word2[0] = %c", word2[0]); //debug
while (index != NULL)
{
printf("\nlookup-while:index->let = %c", index->let); //debug
if (word2[0] == index->let)
{
if (index->words == NULL)
{
printf("\nindex->word == NULL\n"); //debug
printf("Position for word %s: %d\n", word, &(index->words)); //debug
position = &(index->words);
printf("test: %d\n", position); //debug
wordfreq = 0;
break;
}
}
index = index->link;
}
return wordfreq;
}
 

HigherGround

Golden Member
Jan 9, 2000
1,827
0
0
1. The value of position2 stays the same because you never change it. You pass in the address of position2 pointer and assign it the value in the lookup(...) function, however the value of position2 is unchanged. Tell me what exactly is the point of this excerise.

2. char word2[length]; is a no, no. Some compilers will swallow it and may actually work, but if you want this code to be portable and bulletproof don't do it. If you want to allocate memory on the stack use const qualifier ...

const int len = 5;
int array[len]; // ok

or simply get the memory from the heap

int len = 5;
int* array = (int*)malloc(sizeof(int)*len);

3. Know you stdlib :p

code

int length = strlen(word);
char word2[length];
strcpy(word2, word);


could be replaced by


char* word2 = strdup(word);


to be consistent unallocate the memory with free(...) rather than delete [], since some memory analysis tools will mark such unallocations as freed mismatched memory ( harmless, but annoying ).

 

PoitNarf

Junior Member
Oct 21, 2001
12
0
0
This exercise is supposed to reinforce pointer concepts in C. If this was Java I would have no problem, but that's another matter, anyway...

"1. The value of position2 stays the same because you never change it. You pass in the address of position2 pointer and assign it the value in the lookup(...) function, however the value of position2 is unchanged."

I understand what you're saying here, but is there a way to make position2 point to what I point position to inside lookup(...)? That is what I'm trying to do. It seems rather odd to me to do it this way, but this is the way the assignment states. I know there is a way to make position2 point to the same thing I assign position to, I just can't figure it out! :frown: Everyone I've talked to that are working on this assignment have basically given up, but I refuse to. Thanks for all of your help thus far HigherGround!!! Once I figure out this problem in my code I'm home free. Thanks.
 

PoitNarf

Junior Member
Oct 21, 2001
12
0
0
because when I have
*position = index->words;

my code seg faults.

it doesn't seg fault with
position = &(index->words);

however I think I see where you're going with this, thought about changing that around. I'll start messing around with that line a bit more.