C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old September 24th, 2008, 07:38 PM
dd8 dd8 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Sep 2008
Posts: 2 dd8 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 35 m 30 sec
Reputation Power: 0
Unhappy Memory & arrays - Printing char array using printf

Ok , im printing two arrays of characters , one after another (with reading operations in between) first one comes out fine , but the second one comes out ending with the contents of first one. I suspect they are allocated consecutively in memory , and for some reason printing the second one also grabs the first one.
Here is the reading/printing part :

currentPointer=0;

//Skip over empty spaces
while(NumBuf[pointer] ==' ')
{
pointer++;
}

while(NumBuf[pointer] != ' ')
{
first[currentPointer]=NumBuf[pointer];
currentPointer++;
pointer++;
}
printf("Student First : %s \n",first);






//SECOND NAME!!!

currentPointer=0;

//Skip over empty spaces
while(NumBuf[pointer] ==' ')
pointer++;


while(NumBuf[pointer] !=',')
{
second[currentPointer]=NumBuf[pointer];
currentPointer++;
pointer++;
}
printf("\nStudent Second : %s \n",second);

}

And thats the output it gives:

15131227 Kelly Nesolon, 92 22 1 3 3
Student Num 15131227
Student First : Kelly
Student Second : NesolohВЪ=Kelly

14151251 Jenny Craig, 91 31 1 1 3 3
Student Num 14151251
Student First : Jenny
Student Second : CraigohВЪ=Jenny

First string is the input string , discard numbers after comma plz.
As you can see , Student Second gives weird output... Any idea??

Reply With Quote
  #2  
Old September 25th, 2008, 08:20 AM
MaHuJa's Avatar
MaHuJa MaHuJa is offline
Contributing User
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: Dec 2007
Posts: 1,177 MaHuJa User rank is Private First Class (20 - 50 Reputation Level)MaHuJa User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Week 1 Day 21 h 27 m 36 sec
Reputation Power: 8
Send a message via Skype to MaHuJa Send a message via XFire to MaHuJa
A char[] is terminated by a null byte. That's how printf knows how to stop printing. If it's overwritten, then it will continue until it finds another null byte later on, and stops - after printing too much. I guess you know this much already.

Edit: I did have all the information I needed in the original post, I just didn't see it; I kinda just assumed it was done.


I need to know how everything (first,second,pointer,currentpointer, and any other such variables) is declared; including the order.

The clue is that something else is sharing memory space with second from [5] and out. I would guess that this space is probably written to as part of saving the current value of pointer and/or currentpointer (which has been kept in registers for the loop) before the call to printf. However, unless they are char (aka 8 bit int) in size that would also introduce null bytes. (They ARE index ints rather than pointers, right?)



For now, the only recommendation I can truly give is to use C++ rather than C - using high-level classes like the standard library string means it handles all of these gory little details for you and lets you concentrate on the overall program.
Comments on this post
Icon agrees: Good explanation. I also agree on the C++ comment

Last edited by MaHuJa : September 26th, 2008 at 04:55 AM.

Reply With Quote
  #3  
Old September 25th, 2008, 10:37 AM
dd8 dd8 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Sep 2008
Posts: 2 dd8 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 35 m 30 sec
Reputation Power: 0
Here is the code

Oh , and yeah , as much as i want , i cannot use C++ , its a course in C.
Brutal low level C.


#include <stdio.h>

#include <stdlib.h>



int main()

{
//
FILE * pFile;
pFile = fopen ("file","r");



if (pFile==NULL)

printf("Error opening file: my.txt");

else

{

char studNum[8],first[10];
char second[10];
int final,midterm,quiz1,quiz2,quiz3;

double finalGrade;

char NumBuf [100];
int ch;

printf("Got here 1\n");

while((ch = (getc(pFile))) != EOF)
{
int lenght = 0;
while(ch!='\n' && ch!=EOF)
{

if(lenght <= 99)
{
NumBuf [lenght] = ch;
}
ch = (getc(pFile));
lenght++;


}

//If line is longer then 99 , omit it
if(lenght <= 99)
{
if(ch!=EOF)
printf("%s\n",NumBuf);

// BREAKING INTO SEPARATE STRINGS!!!

int pointer = 0;
int currentPointer = 0;

//STUDENT NUMBER!!!
while(NumBuf[pointer] != ' ')
{
if(isdigit(NumBuf[pointer]))
{
// printf("%i",pointer);
studNum[currentPointer]=NumBuf[pointer];
currentPointer++;
}
pointer++;

}
printf("Student Num %s \n",studNum);

//FIRST NAME!!!
currentPointer=0;

//Skip over empty spaces
while(NumBuf[pointer] ==' ')
{
pointer++;
}

while(NumBuf[pointer] != ' ')
{
first[currentPointer]=NumBuf[pointer];
currentPointer++;
pointer++;
}
printf("Student First : %s \n",first);





//SECOND NAME!!!
currentPointer=0;

//Skip over empty spaces
while(NumBuf[pointer] ==' ')
pointer++;

while(NumBuf[pointer] !=',')
{
printf("%i",pointer);
second[currentPointer]=NumBuf[pointer];
currentPointer++;
pointer++;
}
printf("Student Second : %.10s \n",second);



}
else
{
printf("MY SPOON IS TOO BIG\n");
}

}

printf("**** UP IS OVER HERE!!!\n");








}

fclose(pFile);



}

Reply With Quote
  #4  
Old September 25th, 2008, 02:00 PM
Icon's Avatar
Icon Icon is offline
Command Line Warrior
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: Sep 2005
Posts: 1,021 Icon User rank is Private First Class (20 - 50 Reputation Level)Icon User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Weeks 7 h 49 m 32 sec
Reputation Power: 10
You are not null-terminating your strings. Add something like
Code:
studNum[currentPointer] = 0; // or '\0' if you wish
for the studNum and similar lines for first and second. You will need to increase the length of the studNum array to 9 to hold the extra 0 character.

This is exactly what MaHuJa already posted before.
__________________
There is no such thing as C/C++, you either program C or C++

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Memory & arrays - Printing char array using printf


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.

© 2003-2014 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap