|
![]() |
Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming > Application Programming | |
Parameter passing Char to sting (C or C++)
|
||
![]() |
|
|
LinkBack | Thread Tools |
|
|
#1 (permalink) | |||||||||||
|
PC Gamer
![]() |
hello
hope there are some of you guys who know C or C++ anyway i have to do pass a char as a sting (ithink )Code:
#include<stdio.h>
void load(char *name, int *z, int *b, int *t)
{
printf("Name");
gets(name);
printf("enter 3 values ");
scanf("%d%d%d", &(*z), &(*b), &(*t));
}
void print(char name, int x, int y, int z, float avg)
{
printf("%sn", name);
printf("The 3 numbers are %d %d %dn", x, y, z);
printf("the avg is %fn", avg);
}
float calc(int a, int b, int c)
{
int sum;
float avg;
sum = a + b + c;
avg = sum / (float) 3 ;
return avg;
}
void main()
{
int a,b,c;
float avg;
char name;
load(&name, &a, &b, &c);
avg = calc(a, b, c);
print(name, a, b,c, avg);
}
so without the char name the program works fine once i add it it does not work. asks for Name , then the 3 #, and stops get a windows box saying program stooped working. hope you guys can help me. thanks
Last edited by KoolMan : 04-02-09 at 12:52 AM |
|||||||||||
|
|
|
|
|
#2 (permalink) |
|
Case Modder
![]() |
The code failed to allocate an array of chars to read the name into i.e. pointer versus array.
Code:
#define GUESS_AS_TO_HOW_MANY_CHARS_NEEDED_FOR_NAME_BUFFER 256
void main()
{
int a,b,c;
float avg;
char name[GUESS_AS_TO_HOW_MANY_CHARS_NEEDED_FOR_NAME_BUFFER];
load(name, &a, &b, &c);
avg = calc(a, b, c);
print(name, a, b,c, avg);
}
Code:
void load(char name[], int *z, int *b, int *t)
{
printf("Name");
gets(name);
printf("enter 3 values ");
scanf("%d %d %d", z, b, t);
}
void print(char name[], int x, int y, int z, float avg)
{
printf("%sn", name);
printf("The 3 numbers are %d %d %d\n", x, y, z);
printf("the avg is %f\n", avg);
}
__________________
Rich Custom Wooden Case Builder
Last edited by Spotswood : 04-02-09 at 06:15 AM |
|
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
|
|