473,395 Members | 1,474 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

C as an OO Pointer Issue?

I am trying to create a structure. reads a name and stores it to a variable in the structure and then print it. Sounds simple enough. Look at the ouput i got. I am so lost its not even funny...

Expand|Select|Wrap|Line Numbers
  1. E:\COMMAN~1>gcc st.c -o st.exe -std=c99
  2.  
  3. E:\COMMAN~1>st
  4.  
  5.  on NTVDM, specify an inval
  6.  on NTVDM, specify an inval
  7.  Name:  on NTVDM, specify an inval
  8.  Name: on NTVDM, specify an inval
  9.  Name: n NTVDM, specify an inval
  10.  Name:  NTVDM, specify an inval
  11.  Name: NTVDM, specify an inval
  12.  Name: TVDM, specify an inval
  13.  Name: VDM, specify an inval
  14.  Name: DM, specify an inval
  15.  Name: M, specify an inval
  16.  Name: , specify an inval
  17.  Name:  specify an inval
  18.  Name: specify an inval
  19.  Name: pecify an inval
  20.  Name: ecify an inval
  21.  Name: cify an inval
  22.  Name: ify an inval
  23.  Name: fy an inval
  24.  Name: y an inval
  25.  Name:  an inval
  26.  Name: an inval
  27.  Name: n inval
  28.  Name:  inval
  29.  Name: inval
  30.  Name: nval
  31.  Name: val
  32.  Name: al
  33.  Name: l
My code is below... can someone help me with this...
Expand|Select|Wrap|Line Numbers
  1. typedef struct 
  2.     {
  3.          char name[30];
  4.     } Student;     // name of structure 
  5.  
  6. int myStruct_set_values(Student* s, char data[30])
  7. {
  8.  
  9.      for(int i=0;i<29;i++)
  10.     {
  11.         s -> name[i] = data[i];
  12.     }
  13. }
  14.  
  15. int main()
  16. {
  17.  
  18.     Student sOne;
  19.     Student* st_ptr = &sOne;
  20.  
  21.     char input[30];
  22.     myStruct_set_values(st_ptr, input);
  23.  
  24.     sscanf("%s",input);
  25.  
  26.     for(int i=0;i<29;i++)
  27.     {
  28.         printf(" \n Name: %s", &st_ptr -> name[i]);
  29.     }
  30. }
I think this is a pointer issue.. but i am not sure...
Mar 7 '08 #1
9 1296
I changed the sscanf() to scanf(). It accepted the data but i still recieved the same printout.

Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3.  
  4.     Student sOne;
  5.     Student* st_ptr = &sOne;
  6.  
  7.     char input[30];
  8.     myStruct_set_values(&st_ptr, input);
  9.  
  10.     scanf("%s", input);
  11.     printf(" \n Name: %s", sOne.name);
  12. }
The output is below. Why is this?

Expand|Select|Wrap|Line Numbers
  1. E:\COMMAN~1>st
  2. dsefe
  3.  
  4.  Name: n NTVDM, specify an inval
  5. E:\COMMAN~1>
Mar 7 '08 #2
Banfa
9,065 Expert Mod 8TB
The strange output is because the array name is never initialised to anything but you copy it's contents to your structure and the for loop at the end of main. Exactly what is it you are trying to do with this loop?

Also in myStruct_set_values you have used a for loop where you could have used strcpy or memcpy but this does not effect the output.
Mar 7 '08 #3
I did something... but i am still not getting the desired output. now i get a zero.
here is the code and output now...

Expand|Select|Wrap|Line Numbers
  1. typedef struct  
  2.     {
  3.     char name[30];
  4.     } Student;     // name of structure 
  5.  
  6. int myStruct_set_values(Student *s, char data[30])
  7. {
  8.  
  9.      for(int i=0;i<29;i++)
  10.     {
  11.         memcpy(data, s -> name, sizeof(s -> name));
  12.     }
  13. }
  14. int main()
  15. {
  16.  
  17.  
  18.  
  19.     Student sOne, *st_ptr;
  20.  
  21.     st_ptr = &sOne;
  22.  
  23.     st_ptr -> name[0] = '0';
  24.     char input[30];
  25.  
  26.     scanf("%s", input);
  27.  
  28.     myStruct_set_values(&st_ptr, input);
  29.  
  30.     printf(" \n Name: %s", st_ptr -> name);
  31. }
Mar 7 '08 #4
This is the warning message and output i get when i compiled the version of code above.

Expand|Select|Wrap|Line Numbers
  1. E:\COMMAN~1>gcc st.c -o st.exe -std=c99
  2. st.c: In function `main':
  3. st.c:35: warning: passing arg 1 of `myStruct_set_values' from incompatible point
  4. er type
  5.  
  6. E:\COMMAN~1>st
  7. fff
  8.  
  9.  Name: 0
  10. E:\COMMAN~1>
Mar 7 '08 #5
I am starting to think i am talking to myself... I got the program to display the input.. but noe i have another problem even with the max array set at 30, i only get four characters output to the screen...

Expand|Select|Wrap|Line Numbers
  1. typedef struct  
  2.     {
  3.     char name[30];
  4.     } Student;     // name of structure 
  5.  
  6. int myStruct_set_values(Student *s, char data[30])
  7. {
  8.     memcpy(s -> name, data, sizeof(data));
  9.  
  10. }
  11.  
  12. int main()
  13. {
  14.  
  15.  
  16.  
  17.     Student sOne, *st_ptr;
  18.  
  19.     st_ptr = &sOne;
  20.  
  21.     st_ptr -> name[0] = '0';
  22.  
  23.     char input[30];
  24.  
  25.     gets(input);
  26.  
  27.     myStruct_set_values(st_ptr, input);
  28.  
  29.     printf(" \n Name: %s", st_ptr -> name);
  30. }
Mar 7 '08 #6
According to

Expand|Select|Wrap|Line Numbers
  1. int sodata = sizeof(data);
  2. printf("\nThe size of the data element is : %d.\n", sodata);
  3.  
The size of data is 4.. no matter what i set any on the array sizes at data is always 4. How do i fix this so that all the inpute gets written to the screen and not just four characters...???
Mar 7 '08 #7
Banfa
9,065 Expert Mod 8TB
[code]int myStruct_set_values(Student *s, char data[30])
{
memcpy(s -> name, data, sizeof(data));
}
Sorry it had got past my bed time so I went to sleep.

OK mistake of the new and inexperienced here so don't worry I have seen loads of people make this mistake. It stems from the fact that you can't pass arrays to functions so when you wrote

int myStruct_set_values(Student *s, char data[30])

although you appear to have written the 2nd parameter as an array of 30 characters the compiler actually produces a char * (the 30 gets ignored) so this is equivalent to

int myStruct_set_values(Student *s, char *data)

then in your memcpy you use sizeof(data) but data is not the array you think it is, it is a pointer so hence it's size is 4 bytes (a very common pointer size on 32 bit systems).

You will have to find another way to get the size of the data pointed to, you have a couple of choices
  1. use size of the destination sizeof(s->name), this actually is an array so you wont get the problem and you protect the name array in MyStruct from overwriting the end of the buffer, so good. However you then have the caveat that data must point to at least sizeof(s->name) bytes or risk a data access exception.
  2. Give the function a 3rd parameter, the number of bytes to copy. Then data can point to a buffer of any size but you will need to check that the size is not > sizeof(s->name) before using it otherwise you will over-write the end of the buffer in MyStruct.
Mar 7 '08 #8
I dont have my compiler right here in front of me. If i did i would already have the answer to my question. Since the compile returns a size 4.. and that is the direct size of data... if i use &data like this

Expand|Select|Wrap|Line Numbers
  1.  
  2. memcpy(s -> name, data, sizeof(&data));
  3.  
  4. int sodata = sizeof(&data);
  5. printf("\nThe size of the data element is : %d.\n", sodata);
  6.  
  7.  
If data is nothing more than a pointer.. wouldn't the value returned be that of name[30]??

Please correct me if im wrong this is just a theory....
Mar 9 '08 #9
Banfa
9,065 Expert Mod 8TB
I dont have my compiler right here in front of me. If i did i would already have the answer to my question. Since the compile returns a size 4.. and that is the direct size of data... if i use &data like this

Expand|Select|Wrap|Line Numbers
  1.  
  2. memcpy(s -> name, data, sizeof(&data));
  3.  
  4. int sodata = sizeof(&data);
  5. printf("\nThe size of the data element is : %d.\n", sodata);
  6.  
  7.  
If data is nothing more than a pointer.. wouldn't the value returned be that of name[30]??

Please correct me if im wrong this is just a theory....
Sorry about the delay.

firstly data is a pointer, you want to reference it not de-reference it so the correct syntax is sizeof(*data). However like I said in a function declaration/definition the syntax char data[30] is a declaration of a pointer to char (char *) the 30 is ignored sizeof(*data) == 1.

To do what you want you would have to declare a pointer to an array of 30 char and the syntax for that is char (*data)[30]. Then sizeof *data == 30.

try this

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2.  
  3. void Fn1(char data[30])
  4. {
  5.     sprintf(data, "Fn1: %d", sizeof *data);
  6. }
  7.  
  8. void Fn2(char (*data)[30])
  9. {
  10.     sprintf(*data, "Fn2: %d", sizeof *data);
  11. }
  12.  
  13. int main()
  14. {
  15.     char data[30];
  16.  
  17.     Fn1(data);
  18.     puts(data);
  19.     Fn2(&data);
  20.     puts(data);
  21.  
  22.     return(0);
  23. }
Mar 11 '08 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

37
by: Ben | last post by:
Hi, there. Recently I was working on a problem where we want to save generic closures in a data structure (a vector). The closure should work for any data type and any method with pre-defined...
2
by: nifsmith | last post by:
Hi I am creating my own Queue class to learn about Queues and pointers. I have come across a question of two styles and I don't know if there are any dangers associated with them. I coded...
26
by: Meenu | last post by:
Hi, Can two different far pointers contain two different addresses but refer to the same location in memory?
33
by: Ney André de Mello Zunino | last post by:
Hello. I have written a simple reference-counting smart pointer class template called RefCountPtr<T>. It works in conjunction with another class, ReferenceCountable, which is responsible for the...
19
by: =?iso-8859-1?b?VG9t4XMg0yBoyWlsaWRoZQ==?= | last post by:
Coming originally from C++, I used to do the likes of the following, using a pointer in a conditional: void Func(int *p) { if (p) { *p++ = 7; *p++ = 8;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.