473,395 Members | 1,393 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.

Strange behaviour of a character in a structure while printing

Hello, I have a strange problem or the problem is very obvious and I can't see it :) I'm kind of a new C programmer so the chance of the latter is more :)

Anyway, I have a structure like this:

Expand|Select|Wrap|Line Numbers
  1. #define EN_COK_HECE 40
  2.  
  3. struct kel
  4. {
  5.     char kelime[EN_COK_HECE];
  6.     char sesliHarfler[EN_COK_HECE];
  7.     int sesliHarfKonumlari[EN_COK_HECE];
  8. };
And on my program I use a code like this:

Expand|Select|Wrap|Line Numbers
  1. printf("%i ", (kelime->sesliHarfler)[2]);
  2. printf("%i ", (kelime->sesliHarfler)[2]);
  3.  
Yes, same line 2 times. But the strange thing is that they print a different output. The output is like "97 -112". And when I add the same line several times more it does not change (97 -112 -112 -112 -112...). So just the first statement changes the value of the character. But I just do printing, I don't do any assignments, so I cannot understand where the problem is.

I am using Windows platform with MinGW system. And the code above is plain C.
Dec 28 '07 #1
4 1434
gpraghuram
1,275 Expert 1GB
Hello, I have a strange problem or the problem is very obvious and I can't see it :) I'm kind of a new C programmer so the chance of the latter is more :)

Anyway, I have a structure like this:

Expand|Select|Wrap|Line Numbers
  1. #define EN_COK_HECE 40
  2.  
  3. struct kel
  4. {
  5.     char kelime[EN_COK_HECE];
  6.     char sesliHarfler[EN_COK_HECE];
  7.     int sesliHarfKonumlari[EN_COK_HECE];
  8. };
And on my program I use a code like this:

Expand|Select|Wrap|Line Numbers
  1. printf("%i ", (kelime->sesliHarfler)[2]);
  2. printf("%i ", (kelime->sesliHarfler)[2]);
  3.  
Yes, same line 2 times. But the strange thing is that they print a different output. The output is like "97 -112". And when I add the same line several times more it does not change (97 -112 -112 -112 -112...). So just the first statement changes the value of the character. But I just do printing, I don't do any assignments, so I cannot understand where the problem is.

I am using Windows platform with MinGW system. And the code above is plain C.
You are trying to print a single character from a character array and instead of using %c you are using %i(i cant find out what is %i).
It is is printing the ASCII value of the character.
Try to use %c instead of %i and try to run ur code.

Thanks
Raghuram
Dec 28 '07 #2
I did it for debugging purpose. When I change it to %c then I get different characters like getting different numbers:

a Ê Ê Ê Ê Ê....

First one is what it should be but I don't know why it changes into Ê nor continues that way instead of keeping to change everytime I use that statement.
Dec 28 '07 #3
manjuks
72
Kelime is pointer to structure kel. Then check weather you have allocated memory for that pointer(kelime). Then try to access that variables, It will work fine.
Dec 28 '07 #4
Yes, the problem was that I didn't allocated memory for my local variable, which is destroyed when the program returns to the main() function. Thanks...
Dec 29 '07 #5

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

Similar topics

4
by: Tomasz Lisowski | last post by:
Hi, We are distributing our Python application as the short main script (.py file) and a set of modules compiled to the .pyc files. So far, we have always treated .pyc files as portable between...
8
by: manochavishal | last post by:
Hi, I have a structure MAX_ID is 5 /**Structure for Copies*/ typedef struct NodeVideoCopy * NodeVideoCopyPtr ; typedef struct NodeVideoCopy {
11
by: Martin Joergensen | last post by:
Hi, I've encountered a really, *really*, REALLY strange error :-) I have a for-loop and after 8 runs I get strange results...... I mean: A really strange result.... I'm calculating...
20
by: Pilcrow | last post by:
This behavior seems very strange to me, but I imagine that someone will be able to 'explain' it in terms of the famous C standard. -------------------- code -----------------------------------...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
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...

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.