#include<stdio.h>
#include<string.h>
struct s {
char ch[20];
float a;
};
int main()
{
struct s p[10];
int i;
for(i=0;i<10;i++)
{
scanf("%s%f",p[i].ch,p[i].a);
}
}
What is wrong with this code?
Its giving runtime error.
What's the problem?
Most of the errors come from this line.
scanf("%s%f",p[i].ch,p[i].a);
You should use the address of p[i].a, and also restrict the numbers of chars to write in p[i].ch.
scanf( "%19s%f", p[i].ch, &p[i].a );
I haven't touched C code for a while but shouldn't it be something like
scanf("%s%f",p[i].ch,&(p[i].a));
(You have to give the memory address of the variables to the scanf function.)
At the line:
scanf("%s%f", p[i].ch, p[i].a);
You are using p[i].a as a float* (pointer), while it's a float. You're invoking undefined behavior. You probably wanted to do it like this:
scanf("%s%f", p[i].ch, &p[i].a);
Change your code like this:
#include <stdio.h>
#include <string.h>
struct s {
char ch[20];
float a;
};
int main(){
struct s p[10];
int i;
for(i=0;i<10;i++){
scanf("%s%f",p[i].ch, &p[i].a);
}
}
Note that variable a is a float type; you need to pass its memory address when using scanf.
I think the problem is in the p[i].a parameter;
use &p[i].a instead.
Related
#include <stdio.h>
#include <string.h>
struct students{
char name[50];
int age;
int height;
};
int main(int argc, char **argv)
{
struct students manoj;
strcpy(manoj.name, "manojkumar");
manoj.age = 15;
displaymanoj(&manoj); //print testing \n , name , age
return 0;
}
void displaymanoj(struct students *ptr) {
printf("Testing...............DEBUG\n");
printf("%s\t%d\n", ptr->name,ptr->age);
printf("END OF TEST: SUCESS -manoj-");
}
I am learning C and it's working where is use pointer to point to structure variable. I am getting the correct output when I run the program. Just that my Geany IDE giving out some message which I would like to know why.
My Compiler Message as below :
You must declare the functions before calling them.
So your program should look something like
// Includes
// Structure
// Function prototype declaration
// This was what you were missing before
void displaymanoj(struct students *ptr);
int main(int argc, char **argv)
{
...
}
void displaymanoj(struct students *ptr) {
...
}
Since you have the definition of displaymanoj() isn't seen when you call it from main(), compiler implicitly declares one with return type int
which conflicts with the actual one. Note that the implicit declaration has been removed since the C99 standard and is no longer valid.
To fix it:
1) Either move the function displaymanoj() above main()'s definition or
2) Do a forward declaration of displaymanoj().
First of all I know that array A degrades to pointer when we call function f(int a[]) and f(int *p) is same.
BUT:
1.I really need sending by value all array.
2.I really need that sending size is non const in function (but const size in plase we calling function)
I write some example:
http://ideone.com/ZbW0wT
#include <stdio.h>
#define SZ 15
typedef struct {int a[SZ];} rec;
int main(){
void pa(rec);
int value[SZ] ={9,8,7,6,5,4,3,2,1,0};
pa(*(rec*)value);
printf("%u %u\n",sizeof(rec),sizeof(value));
return 0;
}
void
pa(rec b){
int z;
for(z=0;z<SZ;z++){
printf("array[%2d] is %d\n",z,b.a[z]);
}
}
This code work for const size , but how change so pa would get by value some rec which size depend on passed array?
Update: it must by value sended , but not const sized as in Pascal etc , but in true C way , all pass by value not by pointer on 0 element
and function need universal so user can write func(variablesizeArrayOfT) where arg passed by value.
if possible need standard way (C11 or better C99 or better C89 or better K&R), if cant then gcc
UPD2: http://ideone.com/H4XGqC
#include
typedef struct{
int len;
int a[];
} av;
void f(av a){
while(a.len--){
printf("array[%2d] is %d\n",a.len,a.a[a.len]);
}
}
int main(){
int b[]={3,1,2,3};
int c[]={7,1,2,3,4,5,6,7};
f(*(av*)b);
f(*(av*)c);
return 0;
}
all good by probably bug in alignment so size(3 and 7) is right but value of a[] is not
UPD3 see throw gcc -g -c 2ndSRC.c &&objdump -d -M intel -S 2ndSRC.o
it just send only size (b[0] and c[0]) but not all array
An idiomatic way to have arrays containing data of variable length in C is to use a buffer with a maximum size which is known at compile time, is that what you wanted?
#include <stdio.h>
#define MAX_SIZE 15
typedef struct {
int arr[MAX_SIZE];
size_t arr_len;
} rec_t;
void pa(rec_t rec){
for(int z=0; z<rec.arr_len; z++){
printf("array[%2d] is %d\n", z, rec.arr[z]);
}
}
int main(void){
rec_t rec ={
.arr = {9,8,7,6,5,4,3,2,1,0},
.arr_len = 10
};
pa(rec);
}
I finished my programming classes in C, and thought I would write some code down. BOOM! I run into so many problems. I guess the C language is so complicated, even a book can't explain how it works entirely.
This is my problem(I am trying to display something using a pointer)
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
void main()
{
char *a;
system("cls");
*a = {"hello"};
printf("%s",a);
getch();
}
*a = {"hello"};
This dereferenced the pointer and assigned something to that memory location. The reason it crashed is because the pointer was uninitialised, ie it did not point at anything (actually it did point at something, but that something was an undefined memory location).
If you had done the following, your program would have worked:
a = "hello";
The type of a is char*. The type of "hello" is also char*.
You don't give value to a pointer to char this way:
*a = {"hello"};
You have to use:
a="hello";
I don't understand very well what you are trying to do. If you only want to print "hello" in your screen, why do you use a pointer to char? What is the getch() for? You use that function this way: http://linux.die.net/man/3/getch Do you intend to read a character?
I would only do:
#include <stdio.h>
main()
{
printf("hello");
}
What are you exactly trying to do?
This is a good reference guide: http://www.gnu.org/software/gnu-c-manual/gnu-c-manual.html
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
void main()
{
char *a;// a is pointer which address is store in stack and when u initialize with any string then string is store in code section which cant be change
clrscr();
a = "hello";// this is the way to store the string but if when u assign while declaring as char *a= hello this will accept Remember hello is store in code where as address of pointer a is store in stack section
printf("%s", a);
getch();
}
~
just providing another insight for you..
just now I tried this in Dev-C++ 5.6.3 and it works..
if you assign the value directly when you are declaring it, it works:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
int main()
{
system("cls");
char *a = {"hello"};
printf("%s",a);
getch();
return 0;
}
and one more thing, clrscr is not a standard c function (it didn't work in my test), so how about using cls in stdlib like I did.. hope it's useful..
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
typedef struct equipamento {
int codDipositivo;
char nomeEquipamento[40];
char newVar[50];
}Equipamento;
insert(int n, int cat, Equipamento eq[])
{
int codigo;
char newVar[40];
printf("\nNew Var: ");
scanf("%s",&newVar);
eq[n].codDipositivo=newVar;
}
main()
{
Equipamento equipamento[MAX_EQ_DSP];
...a bunch of scanfs
scanf("%d",&n);
scanf("%d",&pr);
insert(n, pr, equipamento);
}
This is a sample of what I have.
on main I have a bunch of scanfs which will update the data showing on the screen but now I want to pass that data into a structure and ask for additional information.
I'm trying to use the updated code but for some reason, instead of 39 chars, it breaks down (returns to the main cycle) after the first char
printf("\nNome do Equipamento: ");
gets(nome);
strcpy(eq[n].nomeEquipamento, nome);
Your problem is this line:
eq[n].codDipositivo=newVar;
In C, you cannot assign arrays, you need to copy them element for element. Remember that C has no string data type, a string is just a NUL-terminated array of char. Luckily there is a function in the C library to help us, strcpy.
strcpy(eq[n].codDipositivo, newVar);
To get the declaration of strcpyyou need to add the following include at the top of your code:
#include <string.h>
char *test = "hello";
test = change_test("world");
printf("%s",test);
char* change_test(char *n){
printf("change: %s",n);
return n;
}
im trying to pass a 'string' back to a char pointer using a function but get the following error:
assignment makes pointer from integer without a cast
what am i doing wrong?
A function used without forward declaration will be considered having signature int (...). You should either forward-declare it:
char* change_test(char*);
...
char* test = "hello";
// etc.
or just move the definition change_test before where you call it.
printf() prints the text to the console but does not change n. Use this code instead:
char *change_test(char *n) {
char *result = new char[256];
sprintf(result, "change: %s", n);
return result;
}
// Do not forget to call delete[] on the value returned from change_test
Also add the declaration of change_test() before calling it:
char *change_test(char *n);
You're converting an integer to a pointer somewhere. Your code is incomplete, but at a guess I'd say it's that you're not defining change_test() before you use it, so the C compiler guesses at its type (and assumes it returns an integer.) Declare change_test() before calling it, like so:
char *change_test(char *n);
thanks a bunch guys! didnt think i would have this problem solved by lunch. here is the final test class
/* standard libraries */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* change_test(char*);
int main(){
char *test = "hello";
test = change_test("world");
printf("%s",test);
return (EXIT_SUCCESS);
}
char* change_test(char *n){
return n;
}