|
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.
|
|
|
|
|
I would be tempted to use strtok() . Then just read the returned value in reverse order.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
|
|
|
|
|
I am developing a c++ application which deals secured information which should not be viewed but to be saved to a file.
Is there a way to write to a file which cannot be viewed directly by the user from disk whereas it could be read by the program.
|
|
|
|
|
manoharbalu wrote:
Is there a way to write to a file which cannot be viewed directly by the user from disk whereas it could be read by the program. Encrypt it?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
|
|
|
|
|
Don't save it on the user's system.
|
|
|
|
|
manoharbalu wrote: Is there a way to write to a file which cannot be viewed directly by the user
no.
|
|
|
|
|
Do what Richard says encrypt it when your write and decrypt when you read it.
You can go from simple XOR encryption if it isn't that important to the real heavy stuff and there is a pile of libraries and code out there.
In vino veritas
|
|
|
|
|
I am using huge structure template and size is around 100kb with various member variables like char array, int, long and short. I am writing this entire structure using fwrite. But when I retreive the structure data from file using fread, some of the structure members especially long variable shows incorrect value.
Kindly suggest me if writing a full structure is wrong or how to work around.
|
|
|
|
|
Without seeing your code it is impossible to guess what you are doing wrong. How are you writing it, with a single fwrite call, or separate calls for each structure item?
|
|
|
|
|
manoharbalu wrote: Kindly suggest me if writing a full structure is wrong
As described probably it is incorrect. Even the design is suspect. Such a large collection must be divisible and as such it should be programmed that way. Note of course that this doesn't mean it cannot be handled as a serialized data stream. That is in fact what all files on disk are exactly.
Other than that it is likely that you have a bug in your code that reads or writes the data. Probably you have a size wrong. If the process was broken into smaller pieces (as I said above) then you could individually test each of those and thus insure that the entire thing was valid.
|
|
|
|
|
Have you made sure you open the file for write in binary mode.
FILE * binfile = fopen("somefilename.bin", "wb"); //Open writable bin file
You open it with just "w" command and it's in text mode some of the raw struct characters will be translated to tabs, CR, LF etc.
In vino veritas
|
|
|
|
|
#include <stdio.h>
#include <string.h>
int input();
int main()
{
char name[100];
printf("Enter your name:\n");
strcpy(name,input());
printf("\n%s", name);
return 0;
}
int input()
{
char c[100];
gets(c);
return(c);
}
i am getting this error while running this code.
[Warning] passing argument 2 of 'strcpy' makes pointer from integer without a cast.
I have tried this code. I know I can do it similarly as that of returning an array from a function to main, but that's too long is there any alternative short method.
|
|
|
|
|
input() returns an int . strcpy() is expecting a char pointer .
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
|
|
|
|
|
how do we use char *function(), and does it returns character, like int function() returns an integer ?
|
|
|
|
|
Tarun Jha wrote: how do we use char *function(), and does it returns character... It returns a pointer to a character.
However, C is not a strong-typed language so you can coerce that into many things. Pointers are a prime example of C being known for giving you just enough rope to hang yourself. Just sayin...
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
|
|
|
|
|
There is no simple method in C to return a string from a function.
Possible methods are:
1. Passing a char* pointer to the function and the function fills the passed buffer. In such cases the size of the buffer should be also passed and the return value indicates failure (usually -1) or success (usually length of copied string):
int input(char *buf, int buf_size)
{
char local_buf[100] = "";
fgets(local_buf, sizeof(local_buf), stdin);
int len = strlen(local_buf);
if (buf_size <= len)
return -1;
strcpy(buf, local_buf);
return len;
}
2. Letting the function allocate a buffer. The calling function must free the buffer when no longer used:
char *input()
{
char local_buf[100] = "";
fgets(local_buf, sizeof(local_buf), stdin);
return strdup(local_buf);
}
char *input_str;
input_str = input();
free(input_str);
However, in your case there is no need for an own function because it is already provided by the standard C library: fgets - C++ Reference[^].
|
|
|
|
|
You can't do this. Because array c is released when input() function execute completed. Maybe you can used "static" key word to prevent array c to released or pass a buffer to input() function as parameter.
|
|
|
|
|
As per your last post lets fix my code from it into a function .. which is what I assume you are trying to do
int StringInput (char* name, int namemax)
{
int c;
int namelen = 0; if (name && namemax > 1) { do {
c = getc(stdin); name[namelen++] = (char) c; } while (c !=EOF && c != '\n' && namelen < namemax-1); temp.name[namelen-1] = '\0'; }
return(namelen); }
/
int main(void)
{
char name[100];
int len = StringInput(&name[0], sizeof(name));
StringInput(&name[0], sizeof(name));
}
Now if this is for uni or commercial then you probably want this form ... An excercise for you is to understand why and how to do it
int StringInput (const char* name, const int namemax, const FILE* source)
In vino veritas
modified 3-Jan-18 21:27pm.
|
|
|
|
|
Quote: if (name && namemax > 1) .
why name is there when it is a char, and how can it be > 1?? And it works fine if i remove it.
|
|
|
|
|
Tarun Jha wrote: Quote: if (name && namemax > 1) .
why name is there when it is a char, and how can it be > 1?? And it works fine if i remove it.
You could rewrite it to be:
if (name != NULL && namemax > 1))
that means that name points to some space in memory and the length of this "space" (character array) is > 1.
And "it works fine if i remove it" only means that name != NULL and namemax > 1
|
|
|
|