![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Feb 2005
Posts: 4
Rep Power: 0
![]() |
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. =/ |
|
|
|
|
|
#2 |
|
Expert Programmer
Join Date: Dec 2004
Posts: 794
Rep Power: 4
![]() |
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().
|
|
|
|
|
|
#3 |
|
Newbie
Join Date: Feb 2005
Posts: 4
Rep Power: 0
![]() |
ah, okay.
still, any ideas why I get a seg fault? =/ |
|
|
|
|
|
#4 |
|
Programmer
Join Date: Feb 2005
Posts: 64
Rep Power: 4
![]() |
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;
} |
|
|
|
|
|
#5 | |
|
Newbie
Join Date: Mar 2005
Posts: 11
Rep Power: 0
![]() |
Quote:
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'. |
|
|
|
|
|
|
#6 |
|
Newbie
Join Date: Feb 2005
Posts: 4
Rep Power: 0
![]() |
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. =/ |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|