473,396 Members | 1,940 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,396 software developers and data experts.

string copy into struct doesn't work

Hi there,
i'm right now using C, IO is done via ncurses, but that won't affect the following problem, i think.
The relevant code is:

-----

Expand|Select|Wrap|Line Numbers
  1. #define SIDEBARWIDTH 27
  2. //...
  3. typedef struct {
  4.     char line[SIDEBARWIDTH];
  5.     int hotkey;
  6.     void (*todo)(int);
  7. } _MENULINE;
  8. //...
  9. char line[SIDEBARWIDTH];
  10. strcpy(line,"Feld auswaehlen    (ENTER)");
  11. _MENULINE *newline;
  12. newline = malloc(sizeof(newline));
  13. strcpy(newline->line,line);
  14. mvwprintw(infowin.win,0,0,"%s",newline->line);
  15. mvwprintw(infowin.win,1,0,"%s",line);
---
For unknown reason those 2 outputlines are not the same. It gives:
Feld auswaeh!^K
Feld auswaehlen (ENTER)

Changing Sidewarwidth to some bigger value won't help that. (actually the string is 26 chars itself, so fits with 27)
Typecasting the mallocoutput doesn't do anything.
Also if i copy charwise via

Expand|Select|Wrap|Line Numbers
  1. for (int i=0;i<SIDEBARWIDTH;i++)
  2.   newline->line[i]=line[i];
nothing changes.

What's going wrong here? malloc allocating somehow wrong? And how could it be fixed?


edited:
i now tried some more if add those lines in the end

Expand|Select|Wrap|Line Numbers
  1. strcpy(line,newline->line);
  2. mvwprintw(infowin.win,2,0,"%s",line);
surprisingly this works, now the new 3rd outputline is correct again. So it seems that the printcommand has some troubles with accessing the struct here.
Not sure if that might be ncurses fault. Still feels odd.
Nov 1 '12 #1
2 1940
donbock
2,426 Expert 2GB
You only malloc enough memory to hold newline (a pointer to _MENULINE), when you really want to allocate enough memory for an instance of _MENULINE.
Nov 1 '12 #2
thanks... that was too easy :(
Nov 1 '12 #3

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

Similar topics

6
by: x muzuo | last post by:
Hi guys, I have got a prob of javascript form validation which just doesnt work with my ASP code. Can any one help me out please. Here is the code: ...
3
by: Clouds | last post by:
Hi ! How do I add the dynamic event handler for a dropdownlist present in the itemtemplate of a datalist !! I am doing it in the itemdatabound event of the datalist but it doesnt work... I am...
2
by: tom | last post by:
how to copy struct to buffer? char * buffer; buffer=(char *)malloc(5*20); Data_O tt; strcpy(tt.Name1, "John ");
0
by: Juna | last post by:
I have been working in vs2003, but now started to work in vs2005 but the problem, I have simple web application not website, which work i mean open in browser when we press F5 or run the...
3
by: jx2 | last post by:
hi guys i would appriciate your coments on this code - when i ran it for the very first time it doesnt see @last = LAST_INSERT_ID() but when i ran it next time it read it properly i need to know it...
1
by: Dany13 | last post by:
hi all. i using some text box for input value and some localvarible for passing this data to dataset . give instance for correct row of dataset and data in data table . use one gird view for...
4
by: supriyamk | last post by:
Hi, i am trying to copy files of a certain type into a different directory using perl, The copy function doesnt seem to work. my @dir_list; my $sub_dir; my $file_name1; ...
9
by: AGP | last post by:
I've been scratching my head for weeks to understand why some code doesnt work for me. here is what i have: dim sVal as string = "13.2401516" dim x as double x = sVal debug.writeline ( x)
3
by: lye85 | last post by:
#include <stdio.h> #include <stdlib.h> #include <string.h> struct account { char *AccName; int Age; double AccBalance; struct account *Next;
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:
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: 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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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.