Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 8th, 2005, 3:01 PM   #1
Duo
Newbie
 
Duo's Avatar
 
Join Date: Feb 2005
Posts: 4
Rep Power: 0 Duo is on a distinguished road
Passing pointers as function arguments? (C)

Hi. I have to write a program that converts a string of numbers, say 6377243 to "message". Sort of like a mobile/cell phone. Anyways, the first problem is converting the numbers to a groups of letters. What I'll figrue I should do is pass the int to a function, which will output an array of char's depending on the value given to it, take pointer to char as input as well, and return the number of elements in the array so I can reference it easily. Problem is, I get a seg fault with this, not sure why. Anyways, here's the code :

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

int
convert (char *c, int i)
{
	int size;

	if (i == 1)
	{
		size = 3;
		c = calloc(size, sizeof(char) );
		c[0] = 'a';
		c[1] = 'b';
		c[2] = 'c';
	}

	return size;
}

int
main (void)
{
	char *c;
	int i, loop;

	i = convert(c, 1);

	for (loop = 0; loop < i; loop++)
	{
		if (loop != i-1)
			printf("%c ", c[i]);
		else 
			printf("or %c.", c[i]);
	}

	return 0;
}

I get a "warning" in ms visual c (not my first choice, but I work with what I have :-( ) about c being used without being initialised. I guess the probelm is that memory isn't actually being allocated to the pointer I pass. I suppose. Though I don't see the point of passing a pointer at all otherwise. Well. Not really sure what's wrong here.

My plan for the whole program is to read in the string from the user, convert, say the first number to the appropriate letters, parse through a text file containing words, then write the appropriate words to an array of pointers to char (since each pointer to char can is basically a string, right?). I would need to calloc space for as many appropriate words first, then actually write to the strings. I might also need to calloc a ***char for each letter in the string, as well.

Oh, well, all of this is immensely obtuse (if you could even make out what I plan to do) and probably not an awfully good way to do it. In fact, it's be way better to do this in Perl or something, but, you know, course work.

Any suggestions as to a better way to do this would be welcome. =/
Duo is offline   Reply With Quote
Old Mar 8th, 2005, 3:34 PM   #2
uman
Expert Programmer
 
Join Date: Dec 2004
Posts: 794
Rep Power: 4 uman is on a distinguished road
Don't worry about the warning. It comes because you pass it to the function before initializing it, but you initialize it in the function with calloc().
uman is offline   Reply With Quote
Old Mar 8th, 2005, 3:53 PM   #3
Duo
Newbie
 
Duo's Avatar
 
Join Date: Feb 2005
Posts: 4
Rep Power: 0 Duo is on a distinguished road
ah, okay.

still, any ideas why I get a seg fault? =/
Duo is offline   Reply With Quote
Old Mar 8th, 2005, 5:55 PM   #4
spydoor
Programmer
 
Join Date: Feb 2005
Posts: 64
Rep Power: 4 spydoor is on a distinguished road
maybe someone can explain it technically, but the seg fault has to do with allocating the memory in a sub function...
also you proably should not be using i for two different things (which number and size). you should create a 2nd variable for size.
Here's an example that works.. sorry I can't explain the memory allocation issue, I'd like to know myself.

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

char * convert (char *c, int i, int *size)
{
    if (i == 1)
    {
        *size = 3;
        c = (char *)calloc(*size, sizeof(char));
        c[0] = 'a';
        c[1] = 'b';
        c[2] = 'c';
    }

    return c;
}

int main (void)
{
    char *c;
    int i, loop, size;

    i = 1;
    c = convert(c, i, &size);

    for (loop = 0; loop < size; loop++)
    {
        if (loop != size-1)
            printf("%c ", c[loop]);
        else
            printf("or %c.", c[loop]);
    }

    return 0;
}
spydoor is offline   Reply With Quote
Old Mar 8th, 2005, 6:33 PM   #5
jorgen_i
Newbie
 
Join Date: Mar 2005
Posts: 11
Rep Power: 0 jorgen_i is on a distinguished road
Quote:
Originally Posted by spydoor
maybe someone can explain it technically, but the seg fault has to do with allocating the memory in a sub function...
.. sorry I can't explain the memory allocation issue, I'd like to know myself.
The address of that memory goes out of scope when the function that allocates it returns, you can fix it by either allocating the memory in the calling function and then passing a pointer to it as an arg to the function that uses it or by declaring the buffer 'static' in the called function.

Thats why we call them 'local' variables...they are only visable to the function that declares them and only for as long as that function is 'alive'.
jorgen_i is offline   Reply With Quote
Old Mar 15th, 2005, 3:36 PM   #6
Duo
Newbie
 
Duo's Avatar
 
Join Date: Feb 2005
Posts: 4
Rep Power: 0 Duo is on a distinguished road
ah, okay, I understand. I wrongly assumed that pointers would be treated differently to normal variables.

also, the using i twice there was pretty silly of me, but I was tired when I wrote that. yeah. that's my excuse.

anyways, another problem! (I haven't been working on this awfully regularly =/)

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

int main (void)
{
	char **words;
	int i, loop;

	printf("Input number of words to be read in!\n");
	scanf("%d", &i);

	words = calloc(i, sizeof(char *));

	for (loop = 0; loop < i; loop++)
	{
		printf("Input word #%d :", loop+1);
		scanf("%s", &words[loop]);
	}

	for (loop = 0; loop < i; loop++)
	{
		printf("%s\n", words[loop]);
	}

	return 0;
}

everything compiles fine, and it seems to take in the values alright, but I get a similar error about memory when it attempts to output the strings I have input. anybody know why?

yes. I have lots to learn. I should probably just get a better book on C. =/
Duo is offline   Reply With Quote
Old Mar 15th, 2005, 4:20 PM   #7
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
I believe you just want words[loop].
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 6:50 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC