Hi Jen, you should use the format options, so that people can see your code better. I did this for you. I also shortened function GetByte, maybe you now can see clearer.
Cubbi is right about GetByte, because, what would happen, if the input string would be longer, than 99 characters.
Cubbi is right about GetByte2. Local variables like temp[100] are allocated on the stack and will be destroyed, when the function returns . For hindering this you could allocate a static array, which will not be destroyed.
You could have written:
static char temp[100];
And Cubbi is right about GetByte3. Only local values, and the last line, which I commented, just for nothing.
Instead of:
//ptr3 = &temp[0];
you also could have written:
//ptr3 = temp;
just for nothing.
Can you see this?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
|
void GetByte(char *ptr)
{
printf("Byte: ");
scanf("%s", ptr);
}
char * GetByte2(void)
{
char temp[100];
printf("Byte2: ");
scanf("%s", temp);
return temp;
}
void GetByte3(char* ptr3)
{
char temp[100];
printf("Byte3: ");
scanf("%s", temp);
//ptr3 = &temp[0];
}
int main(int argc, char *argv[])
{
//- Function #1 ------------------------------
char byte[100];
GetByte(byte);
printf("Byte: %s\n", byte);
//- Function #2 ------------------------------
char byte2[100];
char* p = byte2;
p = GetByte2();
printf("Byte2: %s\n", p);
//- Function #3 ------------------------------
char byte3[100];
GetByte3(byte3);
printf("Byte3: %s\n", byte3);
system("PAUSE");
return EXIT_SUCCESS;
}
|
What youd could have meant by GetByte3:
1 2 3 4 5 6 7
|
void GetByte3(char * * ptr3)
{
static char temp[100];
printf("Byte3: ");
scanf("%s", temp);
*ptr3 = temp;
}
|
and
1 2 3 4
|
//- Function #3 ------------------------------
char * byte3;
GetByte3(&byte3);
printf("Byte3: %s\n", byte3);
|
But I don't know, whether you can think this clearly over
And also some lines of Function 2 in main don't make much sense. Could have been written simply as:
1 2
|
//- Function #2 ------------------------------
printf("Byte2: %s\n", GetByte2());
|
But with some practice you soon will become better
And please use the format option <> next time