|
what condition I can add here to avoid it?
|
|
|
|
|
At the end of the loop you just need to check the last word in array b.
|
|
|
|
|
|
It is essential to "see" the code properly indented and formatted. Then you can recognise - besides the problems mentioned by Richard - that there is a missing closing parentheses (which should also issue a compiler error if not present behind the posted code):
void find_long(char *a)
{
int l,i,l1=0,j=0,l2=0;
char b[50],c[50];
l=strlen(a);
for (i = 0; i < l; i++) {
if (a[i] != ' ') {
b[j] = a[i];
printf("%c ",b[j]);
j++;
}
else if (a[i] == ' ') {
b[j] = '\0';
if (strlen(b) > l1) {
strcpy(c,b);
l1 = strlen(b);
}
j = 0;
}
else {
printf("\nEOF");}
}
}
printf("\nlongest is \n");
printf(c);
}
|
|
|
|
|
Try
#include <stdio.h>
enum State
{
INSIDE_BLANKS,
INSIDE_WORD
};
void find_longest_word( const char * a, const char ** pps, const char ** ppe);
int main()
{
const char * foo = "alpha beta gamma delta epsilon ";
const char *ps, *pe;
find_longest_word( foo, &ps, &pe);
if ( pe-ps > 0)
{
printf("longest word length = %ld\n", (pe-ps));
while (ps != pe)
{
printf("%c", *ps);
++ps;
}
printf("\n");
}
return 0;
}
void find_longest_word( const char * a, const char ** pps, const char **ppe)
{
const char * ps = a;
const char * pe = a;
*pps = *ppe = a;
enum State state = INSIDE_BLANKS;
while ( *a != '\0')
{
if ( state == INSIDE_BLANKS)
{
if ( *a != ' ')
{
state = INSIDE_WORD;
ps = a;
}
}
else {
if ( *a == ' ')
{
pe = a;
if ( pe - ps > *ppe - *pps)
{
*pps = ps;
*ppe = pe;
}
state = INSIDE_BLANKS;
}
}
++a;
}
if ( state == INSIDE_WORD)
{
if ( pe - ps > *pps - *pps)
{
*pps = ps;
*ppe = pe;
}
}
}
|
|
|
|
|
Hi. I have a problem when I am trying to convert CString to CByteArray. Here is the code, and the result:
CByteArray arrByte2;
BYTE* pByteArray = (PBYTE)(LPCTSTR)sSerial;
arrByte2.SetSize(sSerial.GetLength());
memcpy(arrByte2.GetData(), pByteArray, m_sSerial.GetLength());
these values, sSerial, pByteArray and arrByte2 are taken from VS debugger.
Why arrByte2 are having "ýýýý««««««««" on tail ? What I am missing here ?
Thank you for any hint.
modified 9-Jan-18 6:25am.
|
|
|
|
|
It looks like the VS debugger is dumping out additional (or the allocated) memory. Is it for exactly the posted code? Or is arrByte2 declared somewhere else and has been used before?
You are also copying m_sSerial.GetLength() bytes while the array size has been set to sSerial.GetLength() . If m_sSerial is a longer string, you will have a buffer overrun.
Finally, your casting should be
const BYTE* pByteArray = reinterpret_cast<const BYTE*>(sSerial.GetString()); That will at least keep the constness of the string but fail too for Unicode builds when sSerial is not explicitly declared as CStringA .
You might call arrByte2.GetSize() and ignore the additional output if that is as expected.
|
|
|
|
|
Flaviu2 wrote: Why arrByte2 are having "ýýýý««««««««" on tail ? What I am missing here ?
Thank you for any hint.
It does NOT have this trash "on tail". This trash is behind the arrByte2.
Debugger interprets its content as characters and cannot find the actual "end" of array since you did not copy the terminated NULL from the CString.
|
|
|
|
|
You need to copy the length of the string + 1 to get the ending '\0' to terminate a C string
Very basic .. without it the string keeps going in C terms which is exactly what the debugger is showing with rubbish that was in the buffer.
In vino veritas
|
|
|
|
|
Thank you all of you. Yes, that was the problem, I should put
arrByte2.SetSize(sSerial.GetLength() + 1);
|
|
|
|
|
Hi
It is my understanding that local variables of a Multithread application i.e those declared on the stack for each function are thread safe. Each thread BP,SP registers have a unique address so variables declared on the stack are all unique to each thread. Am I correct in understanding this ?
Thanks
|
|
|
|
|
Each thread has its own stack. When calling a function from different threads, the local function variables reside on the stack of the thread from where the function has been called. So it is thread safe.
But note that this does not apply to local static variables. While also local variables (can't be accessed from outside of the function), there is only one instance of such variables. Accessing them must be guarded (locks, atomic operations) and the function must not return a pointer to them to make the function thread safe.
|
|
|
|
|
Thanks I think of static local variables as global. I am using a local variable as a switch if local variable BOOL foo = TRUE for thread A, It (BOOL foo is not the same) for thread B
is there any reason in a Multithread application I would have to guard them (local variables non static ) there are no references to them by pointers
Thanks
|
|
|
|
|
You only have to guard shared variables (like static variables). Variables on the stack are safe.
|
|
|
|
|
THANKS SO MUCH
|
|
|
|
|
I want to print elements of array a.
My print fun is :
void print(int *a){
int i;
for(i=0;i<3;i++){
printf("%d\n",a[i]);
}
}
which works fine. But if I give :
void print(int *a[]){
int i;
for(i=0;i<3;i++){
printf("%d\n",a[i]);
}
}
(array a[] defined in main.)
its not working.What makes the difference?
|
|
|
|
|
The difference is in the prototypes :
void print(int *a) vs
void print(int *a[])
The first takes a pointer to integer. The second takes an array of pointers to integers.
To make the second function work you have to dereference a pointer :
printf( "%d\n", *a[i] );
|
|
|
|
|
the equivalent of
void print(int *a)
with array notation is
void print(int a[])
|
|
|
|
|
void print(int *a[]):
It looks like a 2 dimensional array
I think so.
|
|
|
|
|
No, it's a one-dimensional array of pointers.
|
|
|
|
|
I am Using GCC running on Linux / Ubuntu
gcc version 5.4.0 20160609 (Ubuntu/Linaro 5.4.0-6ubuntu1~16.04.4)
I need / like to have a single bit variable / flag passed to / from SPI hardware.
Single bit defined
// need for SPI /I2C
typedef unsigned char bit : 1; // single bit
Implemented
char x;
static bit b; // defined in generic def header
Compiler error - what initializer is missing?
In file included from ../src/MODULES/M_SPI/CASPI.h:53 ,
from ../src/MODULES/M_SPI/CASPI.cpp:34:
/media/os64/Eclipse/eclipse/Workspace/Eclipse_Oxygen_1A/VNA_2/src/MODULES/MODULE_1602/C_GenericTypeDefs.h:77:33: error: expected initializer before ‘:’ token
typedef unsigned char bit : 1; // single bit
^
../src/MODULES/M_SPI/CASPI.cpp:239:1: error: ‘bit’ does not name a type
bit C_SPIClass::I2CTX(unsigned char d) {
^
Thanks for any help.
Cheers
|
|
|
|
|
You can only have bit fields in a struct or union, it would be physically stupid to have them outside a struct or union because they couldn't be packed anyhow you might as well pick any normal type. Your somewhat pointless definition (it saves zero memory space) would be
typedef struct {
unsigned bit : 1; // single bit
} BIT;
The initialize would be
static BIT bit = { 0 };
or
static BIT bit = {.bit = 0};
The only point to bit fields is to save space or make naming and offsets easier in particular for hardware registers.
The other recommendation I make is only define them as sign or unsigned you don't have to assign a normal C type the
compiler internally knows how to cast bit fields to do operations on them.
Usually what you are doing on hardware registers is assigning the bits to match something like an 8 bit definition
typedef struct __attribute__((__packed__, aligned(1))) {
unsigned Clk : 1; // clk bit
unsigned Bit : 1; // Data bit
unsigned CS : 1; // Chip select
unsigned _reserved: 5; // 5 unused bits
} MYREGISTER;
In the above you have 8 bits packed into a byte so it saves space carrying them.
In vino veritas
modified 7-Jan-18 21:41pm.
|
|
|
|
|
|
for ex,
input: My name is Bay Max.
output: Max Bay is name My.
below is my code, but its too long and messy. How do i make it more concise and readable?
Any alternative would be appreciated.
#include <stdio.h>
#include <string.h>
#define EOL '\n'
#define MAX_SIZE 100
int main(){
char text[MAX_SIZE], back[MAX_SIZE], temp[MAX_SIZE];
int count, i, mark[50];
for(count=0; (text[count] = getc(stdin))!= EOL; ++count){
mark[count]=0;
}
text[count] = '\0';
for(i = count-1; i>=0; --i){
back[(count-1)-i] = text[i];
}
back[count] = '\0';
for(i=count-1; i>=0; --i){
if(back[i]== ' '){
mark[i] = i;
}
}
int j, k=0, l=0;
for(i=0; i<count-1; i++){
if(mark[i] != 0){
for(j=mark[i]-1; j>=k; j--){
temp[l] = back[j]; l++;
}
temp[mark[i]] = ' ';
l= l+1;
k = mark[i]+1;
}
}
for(i=count-1; i>=k; i--){
temp[l] = back[i]; l++;
}
printf("\n%s", temp);
return 0;
}
Thank you.
|
|
|
|
|
I'd make a map of the words (array of pointers to char), then iterate it backward.
|
|
|
|