•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 396,965 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,967 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser:
Views: 340 | Replies: 5 | Solved
![]() |
•
•
Join Date: Apr 2008
Posts: 22
Reputation:
Rep Power: 1
Solved Threads: 0
Hi, I've have a problem of printing data from stack, if it's simple words,then stack works just fine,but if I try to push changed data using function(Written by a very intelligent moderator
), I get just the same word n times:
Intelect
Intelect
Intelect
....
So, should I try to change stack or maybe there is another/easier way?
), I get just the same word n times:Intelect
Intelect
Intelect
....
#include<stdio.h>
#include<stdlib.h>
#include <string.h>
//-------------------Function--------------------------------------------
char AD(char *record, char *record_out)
{
char *p1 = record;
char *p2 = record_out;
while( *p1 )
{
// use temp pointer to locate
// end of digits
char* p3 = p1;
while( isdigit(*p3) )
p3++;
// is there a space following the digits ?
if( *p3 == ' ')
// yes, then skip the space and move on to the next char
p1 = p3+1;
else
{
// no, then was the last char put into temp buffer
// a space
if( *(p2-1) == ' ')
// yes, then overwrite that space with a new character
p2--;
p1 = p3;
}
// copy all characters up to the next digit
while(*p1 && !isdigit(*p1) )
*p2++ = *p1++;
}
}
//------------------------------------------------------------------------------
struct stack {
char *d;//data
struct stack *next;//pointer
};
typedef struct stack ELEMENT;
typedef ELEMENT *POINTER;
void push(POINTER *Top, char *a)
/* put a into the top of the stack */
{
POINTER temp;
temp = malloc(sizeof(ELEMENT));
temp->d= a;
temp->next = *Top;
*Top = temp;
}
void print_stack(POINTER Top){
POINTER copy= Top;
while(copy!=NULL)
{
printf("%s\n",copy->d);
copy=copy->next;
}
}
int main()
{
POINTER start = NULL;
char *DELIMITERS = " ";
char *str,record[100],*o;
char line[]="jjj yyyy dfsfsd sdfsdf sdfsdfsdf sdfsdfsfd sdfasdfafs Intelect";
str = strtok(line, DELIMITERS);
char recordd[sizeof(record)] = {0};
while (str)
{
AD(str, recordd); //Changing word with function(actually line,but if it works with word,then...good)
// printf("%s\n",recordd);
o = recordd;
// str=o; // But if I just try to push changed word(With function which IS needed) function
// print_stack(start) prints stack for n times ????
push(&start,str); //push the word ... and this part works!!!
str = strtok(NULL, DELIMITERS);
}
print_stack(start); //print stack..works, print changed words - don't...
system("pause");
} Last edited by Onixtender : May 12th, 2008 at 3:38 pm.
•
•
Join Date: May 2008
Location: Infinite Castle, below Beltline
Posts: 282
Reputation:
Rep Power: 1
Solved Threads: 22
•
•
Join Date: Nov 2007
Posts: 842
Reputation:
Rep Power: 4
Solved Threads: 172
<< if I try to push changed data using function, I get just the same word n times:
That happens because you are pushing pointers to one single buffer you have in your whole program, and that buffer is char recordd[].
In other words, you can write to that buffer as many times as you wish, but the data that was last written to it, is the only data you ever can get out of it.
So, you need to allocate dynamic memory into which you copy the AD()'s output, that you eventually push onto the stack. Maybe you get the idea from the following ...
That happens because you are pushing pointers to one single buffer you have in your whole program, and that buffer is char recordd[].
In other words, you can write to that buffer as many times as you wish, but the data that was last written to it, is the only data you ever can get out of it.
So, you need to allocate dynamic memory into which you copy the AD()'s output, that you eventually push onto the stack. Maybe you get the idea from the following ...
int main()
{
POINTER start = NULL;
char *DELIMITERS = " ";
char *str,record[100],*o;
int len; // holds the length of string that will be pushed
char line[]="jjj yyyy dfsfsd sdfsdf sdfsdfsdf sdfsdfsfd sdfasdfafs Intelect";
str = strtok(line, DELIMITERS);
// Consider recordd as just an intermediate work buffer
// for the AD() function
char recordd[sizeof(record)] = {0};
while (str)
{
// Pass the input to AD()
AD(str, recordd);
// Get the length of string and account for a terminating null char
len = 1 + strlen(recordd);
if(len > 1) // did AD() give some output?
{
// Allocate the memory needed to hold the string to push
char * temp = (char *)malloc(len * sizeof(char));
if(temp)
{
// malloc() succeeded, now copy the data from recordd
strcpy(temp, recordd);
// and push it
push(&start, temp);
}
}
str = strtok(NULL, DELIMITERS);
}
print_stack(start);
system("pause");
}![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb C Marketplace
Similar Threads
- Odd Page Cannot Be Displayed Problem (All browsers, not just IE) (Windows NT / 2000 / XP / 2003)
- Derived class problem (C)
- Stack Queue Fstream (C++)
- Reverse Output (Stack) (C++)
- Start button and Quick Launch problem on boot (Windows 9x / Me)
- TCP/IP stack whacked by malware; no DNS resolution (Windows NT / 2000 / XP / 2003)
- internet explorer problem (Web Browsers)
Other Threads in the C Forum
- Previous Thread: File Integirty Check
- Next Thread: Timestamp


Linear Mode