473,399 Members | 4,177 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,399 software developers and data experts.

Struct, char array and pointer problem.

EARNEST
128 100+
Expand|Select|Wrap|Line Numbers
  1. #include <string.h>
  2. #include <ncurses.h>
  3. #include <time.h>
  4. #include <stdlib.h>
  5.  
  6. typedef struct builder{
  7.     int locX;
  8.     int locY;
  9.     char charValue_H;
  10.     char charValue_T;
  11.     char *charArray_B;
  12. } sbuilder;
  13.  
  14. void drawborders();
  15. void initialize(sbuilder *stopass);
  16. bool collisiondetection();
  17.  
  18. int main()
  19. {
  20.     initscr();
  21.     noecho();
  22.     cbreak();
  23.     sbuilder ss, *ptss;
  24.     ptss = NULL;
  25.     ptss = (sbuilder *) malloc (sizeof(sbuilder));
  26.  
  27.     initialize(ptss);
  28.     initialize(&ss);
  29.  
  30.     mvprintw(10,10, "string is %s and %s", ss.charArray_B, ptss->charArray_B); //incorrect
  31.  
  32.     getch();
  33.     refresh();
  34.     endwin();
  35.     return 0;
  36. }
  37.  
  38. void initialize(sbuilder *s)
  39. {    
  40.     //initial location
  41.     s->locY = LINES/2;
  42.     s->locX = COLS/2;
  43.     s->charValue_H = 'H';
  44.     s->charValue_T = 'T';
  45.  
  46.     int i;
  47.     char s_initial[4];
  48.  
  49.     for (i = 0; i <= (sizeof(s_initial)-3); i++) //last loc  is /0
  50.     {
  51.         s_initial[i] = s->charValue_T;    
  52.     }
  53.  
  54.     s_initial[i] = s->charValue_H;
  55.  
  56.     s->charArray_B = s_initial;
  57.  
  58.     mvprintw(1,1, "string is %s", s->charArray_B);  //correct
  59. }
  60.  
  61.  
  62.  
Correct output : TTH
Incorrect: weird symbols
Also, I wanted that function to return a struct, but i had similar problems.
Any hints, please?
Mar 30 '10 #1

✓ answered by weaknessforcats

s_initial does not have a string in it. Even if it did, strlen would return a maximum of three.

If it's 4 that you want, then code 4. If this is variable, then here's where I wouild use an argument for the Create() function:

Expand|Select|Wrap|Line Numbers
  1. sbuilder* Create(int arraysize)
  2. {
  3.      sbuilder* temp = malloc(arraysize * sizeof(char));
  4.      initialize(temp, arraysize);
  5.      return temp;
  6. }
This way the initialize funcition can just use the arraysize passed to allocate the array on the heap. You shouldn't need the local array at all.

13 6976
weaknessforcats
9,208 Expert Mod 8TB
In your initialize function you have s->charArray_B = s_initial. But s_initial is a local variable.

You need to allocate this on the heap.

I suggest a create function that a) creates your struct variable on the heap, b) calls initialize to initialize all the struct members and c) returns the addressof that struct variable. Then write a delete to free the struct variable

In main():

Expand|Select|Wrap|Line Numbers
  1. sbuilder* ptr = Create();  //you could pass initial values as arguments
  2.  
  3. //and you are set to go.
  4.  
  5. //Finally, when you are done:
  6.  
  7. delete(ptr);
  8.  
The Create() looks like:

Expand|Select|Wrap|Line Numbers
  1. sbuilder* Create()
  2. {
  3.     sbuilder* temp = malloc(sizeof(sbuilder);
  4.     Initialize(temp);
  5.     return temp;
  6. }
Mar 30 '10 #2
EARNEST
128 100+
I managed (kinda) to solve it. Is it the right way?
Expand|Select|Wrap|Line Numbers
  1.     //initial location
  2.     s->locY = LINES/2;
  3.     s->locX = COLS/2;
  4.     s->charValue_H = '@';
  5.     s->charValue_T = '#';
  6.  
  7.     int i;
  8.     char s_initial[4];
  9.  
  10.     for (i = 0; i <= (sizeof(s_initial)-3); i++)
  11.     {
  12.         s_initial[i] = s->charValue_T;    
  13.     }
  14.     s_initial[i] = s->charValue_H;
  15.     s->charArray_B = (char *)malloc((strlen(s_initial))*sizeof(char));    
  16.     strcat(s->charArray_B, s_initial); 
  17.  
?
Mar 30 '10 #3
weaknessforcats
9,208 Expert Mod 8TB
s_initial does not have a string in it. Even if it did, strlen would return a maximum of three.

If it's 4 that you want, then code 4. If this is variable, then here's where I wouild use an argument for the Create() function:

Expand|Select|Wrap|Line Numbers
  1. sbuilder* Create(int arraysize)
  2. {
  3.      sbuilder* temp = malloc(arraysize * sizeof(char));
  4.      initialize(temp, arraysize);
  5.      return temp;
  6. }
This way the initialize funcition can just use the arraysize passed to allocate the array on the heap. You shouldn't need the local array at all.
Mar 30 '10 #4
EARNEST
128 100+
thanks, i used #define ARRAYSIZE 4;
Mar 30 '10 #5
EARNEST
128 100+
Should my initialize have (sbuilder *varpass) ?
Mar 30 '10 #6
EARNEST
128 100+
I have a problem. Cant figure out, any help, please:
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <ncurses.h>
  4.  
  5. typedef struct testing
  6. {
  7.     int aa;
  8.     int bb;
  9. } loc;
  10.  
  11. typedef struct useoftests
  12. {
  13.     char tchar;
  14.     loc *arrayoftests;
  15. }tester;
  16.  
  17. tester *create_testcase();
  18. void init(tester *temper);
  19.  
  20. int main()
  21. {
  22.     initscr();
  23.     tester *testerINST;
  24.  
  25.     testerINST = create_testcase();
  26.  
  27.     refresh();
  28.     getch();
  29.     endwin();
  30.     return 0;
  31. }
  32.  
  33. tester *create_testcase()
  34. {
  35.     tester *temptester;
  36.     temptester = malloc(sizeof(tester));
  37.     init(temptester);
  38.     return temptester;
  39. }
  40.  
  41. void init(tester *temper)
  42. {
  43.     int ss;
  44.  
  45.     temper->arrayoftests = (loc *)malloc(15*sizeof(loc));    //malloc for body
  46.  
  47.     ss = sizeof(temper->arrayoftests);
  48.     mvprintw(5,2, "size : %d", ss);
  49. }
  50.  
The output is 4 :/
Mar 31 '10 #7
weaknessforcats
9,208 Expert Mod 8TB
arrayoftests is a pointer. The sizeof the pointer is 4.

You cannot show the size of an array by using sizeof.

sizeof shows the value of the variable on the stack. If you have a local array:

Expand|Select|Wrap|Line Numbers
  1. int array[6];
and you sizeof(array) you will get 24 (65 integers at 4 bytes each).

But if you pass the array to a function, what is passed is the address of element 0. Ahen you do a sizeof(array) inside the function you get 4, which is the sizeof the address of element 0.

Likewise, when you create an array on the heap using the new operator or malloc what is returned is the address of element 0, which again is 4.

Almost certainly you will need a separate variable for the number of elements.
Mar 31 '10 #8
EARNEST
128 100+
Hm, you mean to get the size of an actual array in one function, and pass that variable to another function?

I am new to C, and I like it and very interested in it, but sometimes I think I want to break my screen :))))
Also, how come my string is not empty and it has weird symbols? How can i fix it? line 64, on ubuntu 9.10 everything is fine, however, freebsd 6.2 gives me weird symbols. cheers.
Expand|Select|Wrap|Line Numbers
  1. #include <string.h>
  2. #include <ncurses.h>
  3. #include <time.h>
  4. #include <stdlib.h>
  5.  
  6.  
  7. typedef struct foo
  8. {
  9.    int var1;
  10.    int var2;
  11. } foos;
  12.  
  13. typedef struct superfoo
  14. {
  15.     int abc;
  16.     char *arrayofchars;
  17.     foos *yesfoos;
  18. } superfoos;
  19. superfoos *init_tester(int ly, int lx);
  20. void init_second(int y, int x, superfoos *temp);
  21.  
  22. int main()
  23. {
  24.     initscr();
  25.  
  26.     int locy;
  27.     int locx;
  28.  
  29.     superfoos *beta;
  30.  
  31.     locy = LINES/2;
  32.     locx = COLS/2;
  33.  
  34.     beta = init_tester(locy,locx);
  35.  
  36.     mvprintw(20,20, "y:%d and  x: %d", beta->yesfoos[0].var2, beta->yesfoos[1].var2-1);
  37.     mvprintw(5,5, "if true, head-1 is %d, head is %d", beta->yesfoos[0].var2, beta->yesfoos[1].var2-1);
  38.  
  39.     getch();
  40.     refresh();
  41.     endwin();
  42.     return 0;
  43. }
  44. superfoos *init_tester(int locy, int locx)
  45. {
  46.     superfoos *temp;
  47.     temp = malloc(sizeof(foos));
  48.     init_second(locy, locx, temp);
  49.     return temp;
  50. }
  51.  
  52. void init_second(int locy, int locx, superfoos *temp)
  53. {
  54.     foos tempfoo[3];
  55.  
  56.     tempfoo[1].var1 = locy;
  57.     tempfoo[1].var2 = locx;
  58.  
  59.     tempfoo[0].var1 = locy;
  60.     tempfoo[0].var2 = --locx;
  61.     char *tempstring = "booboo";
  62.     temp->yesfoos = (foos *) malloc((sizeof(tempfoo)/sizeof(foos))*sizeof(foos));//malloc for body locations
  63.     temp->arrayofchars = (char *)malloc(strlen(tempstring)*sizeof(char));
  64.     mvprintw(10,15, "string before is %s%s", temp->arrayofchars,".");
  65.     memcpy(temp->yesfoos, tempfoo, sizeof(tempfoo)); //assigning locations
  66.  
  67.  
  68. }
  69.  
Mar 31 '10 #9
weaknessforcats
9,208 Expert Mod 8TB
You seem to be going to a lot of toruble.

All you need do is a) allocate mempory and b) copy the data to that memory.

Note that oif your allocation is to contain a string then you need 1 additional byte to hold the \0 null terminator. Like this:


Expand|Select|Wrap|Line Numbers
  1. char* data = "Earnest";  /* the initial data */
  2.  
  3. char array = malloc((strlen(data) +1)* sizeof(char));
  4. strcpy(array, data);
strlen return the number of characters in a string,. In this case 7 since there are 7 characters in Earnest. Therefore, you need an 8 character array to told a 7 character string. Then just copy the data into your allocation.

It helps in C when working with strings to stick with the str... functions that are all set up to work with strings.
Apr 2 '10 #10
EARNEST
128 100+
Hm, talking about pointers and th 0th location size. Is there a way to pass a struct that has lets say (int a, int b, struct structname2 *ssss) to another function and so that function would have access to all that data and store it to a shared memory segment, so other processes could read it? i've tried, but had problems with segmentation :(
Apr 7 '10 #11
weaknessforcats
9,208 Expert Mod 8TB
You will need to read up on shared memory management for the operating system that you are using.

Keep in mind that pointer addresses are valid only from your process and you can't use those addresses in another process.
Apr 7 '10 #12
EARNEST
128 100+
its ubuntu 9.10. trying to write a structure to a segment
Apr 7 '10 #13
weaknessforcats
9,208 Expert Mod 8TB
Well, that's it for me. I don't know the first thing about ubuntu.
Apr 7 '10 #14

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

Similar topics

4
by: jagmeena | last post by:
Hello, I am sure this problem has been addressed before, however, I could'nt get a suitable solution to my problem. Hence I am posting here. Thanks a lot for all your help. The code I have is ...
5
by: Alex Vinokur | last post by:
"Richard Bos" <rlb@hoekstra-uitgeverij.nl> wrote in message news:4180f756.197032434@news.individual.net... to news:comp.lang.c > ben19777@hotmail.com (Ben) wrote: > > 2) Structure casted into an...
5
by: Roy Hills | last post by:
When I'm reading from or writing to a network socket, I want to use a struct to represent the structured data, but must use an unsigned char buffer for the call to sendto() or recvfrom(). I have...
19
by: Geetesh | last post by:
Recently i saw a code in which there was a structer defination similar as bellow: struct foo { int dummy1; int dummy2; int last }; In application the above array is always allocated at...
15
by: fix | last post by:
Hi all, I am writing a program using some structs, it is not running and I believe it is because there's some memory leak - the debugger tells me that the code causes the problem is in the malloc...
2
by: aap | last post by:
I have the following code #define MAX 32 struct A { char carr; int iarr; int i; }; void main() {
25
by: SRR | last post by:
Consider the following code: #include <stdio.h> #include <string.h> struct test{ char a; } funTest( void ); int main( void ) {
19
by: rmr531 | last post by:
First of all I am very new to c++ so please bear with me. I am trying to create a program that keeps an inventory of items. I am trying to use a struct to store a product name, purchase price,...
14
by: ManicQin | last post by:
Hi all. I'm trying to get the size of a variable in a struct by his relative postion i.e. /// #define offsetof(s,m) (size_t)&(((s *)0)->m) struct ThePimp{ char rings; char blings;
19
by: bowlderyu | last post by:
Hello, all. If a struct contains a character strings, there are two methods to define the struct, one by character array, another by character pointer. E.g, //Program for struct includeing...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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:
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.