spot7.org logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML Categories
  Home » C » Page 1
Fatal error while printing a 2 dimensional array
the following compiles #include <stdio.h> #include <stdlib.h> // contains rand and srand function prototypes #include <time.h> //#include "gbgb.h" void generate( void ); #define UP 0x01U #define RIGHT 0x02U #define DOWN 0x04U #define LEFT 0x08U typedef unsigned char UBYTE; int occupy[8][8] = { {0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0}, {0,0,0,0

Categories : C

What is the pointer pointing to?
When I execute this on the pointer s, then in the end does it point to ''? No, it does not. Due to post-incrementing, at the end of the loop s points one char past ''. If I want to refer to the second last element of the array do I use *(s-2)? That would be the last character of the string, assuming that your C string is not empty How do I print out all the characters stored in the

Categories : C

Generating a Sudoku Board in C
Since this is a homework I will give you a little hint, not the solution. display[i] = grid[rand() % 9 + 1]; here you copy from a random grid index to display[i]. This is definitely not what you want. When you copy you need to copy to the same index. I.e. display[i] = grid[i]; You just have to choose which i to copy and witch not. You need to generate 4 random numbers from 9 numbers. For in

Categories : C

reading words/strings from file+length of them - c
They are ALOT of errors with the code you shared : J is not declared, so you need to add int j = 0; I'm assuming than j is the number on whitespace on your doc. word[b]=word[b]+c; get changed into word[b]= c; You add an incremntation on b in your loop then, so you wont write only on word[0]. Your printing is bad aswell, you would only show the first letter over and over. This is the final co

Categories : C

Is my interpretation correct?Can I print binary representation for double, as eg:1?
Your code actually displays the correct result. 0.5=1/2 so 23.5 has only one "two-cimal". It is not the case of 0.4, which has an infinite number of "two-cimal". With printf(), you may print an interger using its hexadecimal (hexa=16) representation with flag %x and a double using its hexadecimal floating point representation with %a. Use %.9a to display 9 "16-cimals". A piece of code to test

Categories : C

order of evaluation for multiple increment operator on pointer
++ operator has higher precedence over * So the pointer will be incremented first and then dereferenced. *p++ First p++ Then *p++ For ++ there has to be a value which needs to be incremented. But the below expression doesn't provide the lvalue for the ++ . p++ is not a modifiable lvalue. *ptr++++;

Categories : C

Looking for algorithm to find n unset bits in a bitset
I think you can do this in 2 passes: Pass #1: Scan the array, noting the number of consecutive zero-bits, and the start of those bits. From your example, the scan would produce: 2 bits, starting at 0 6 bits, starting at 3 3 bits, starting at 12 4 bits, starting at 18 Pass #2: Scan the data from pass #1, looking for the target value (4), or the smallest value larger than the target. Both pass

Categories : C

Pthreads - Increase maximum number of read locks
You are probably forgetting to release the read lock. Because given pthread_rwlock_t definition (x86_84): typedef union { struct { int __lock; unsigned int __nr_readers; unsigned int __readers_wakeup; unsigned int __writer_wakeup; unsigned int __nr_readers_queued; unsigned int __nr_writers_queued; int __writer; int __shared; unsigned long int __pad1; u

Categories : C

Write one billion files in one Folder BUT "(No space left on device)" error
tune2fs -l /path/to/drive Filesystem features: has_journal ext_attr resize_inode dir_index filetype needs_recovery extent flex_bg sparse_super large_file huge_file uninit_bg dir_nlink extra_isize Filesystem flags: signed_directory_hash Default mount options: user_xattr acl Filesystem state: clean Errors behavior: Continue Filesystem OS type: Linux Ino

Categories : C

Compiler doesn't stop on scanf() for input
Just put a space in the scanf like this: scanf(" %c",&ch); And the reason is you enter: Y (with the enter) scanf only reads Y! So in the next scanf is still in the buffer and get's read in! So if you now have a space before you read something new it makes a cut (and it looks like this: ' ')!

Categories : C

Line Counter In C
Apparently the implementation is supposed to count only the number of newline characters, not the number of total characters, as implemented in the if(c==' ') condition. The program returns 0 on your input as it does not contain a newline character.

Categories : C

Reasigning a pointer?
Thank you Jeremy and Lundin. In case someone else stumbles on something similar, changing unsigned int temp1_value; to volatile unsigned int temp1_value; gives me the expected behavior.

Categories : C

The last character is not printed to a file
As mentioned in the comments: fprintf(desFile, "%s",data2[numOfHeaders]); //okay printf(desFile, "%s",data2[numOfHeaders]); //wrong Second line should be: printf("%s",data2[numOfHeaders]); //okay Or, you could do this: sprintf(buffer, "%s",data2[numOfHeaders]); fprintf(desFile, buffer); printf(buffer); Other possible issues: Without an input file it

Categories : C

How do I empty the SDL event queue?
You should be checking event's in a while loop using polling which will pop the events from the queue as they are handled. Use either multiple if/then statements or a swicth block for handling the event. I recommend this method as you can easily miss important events such as SDL_QUIT when using SDL_PeepEvent Basic example: SDL_Event e; while (SDL_PollEvent(&e)) { if (e.type == SDL_QUIT)

Categories : C

Tilde operator in C
When you pass an argument to printf and that argument is of integer type shorter than int, it is implicitly promoted to int as per K&R argument promotion rules. Thus your printf-call actually behaves like: printf("%u ", (int)~i); Notice that this is undefined behavior since you told printf that the argument has an unsigned type whereas int is actually a signed type. Convert i to unsigned sh

Categories : C

Data type for holding the count of elements in a data structure
It seems to me that, in your case, it's more natural keep size_t and use ptrdiff_t instead of int for the function arguments and return values. You can find it in stddef.h and as the name suggests, it's intended to be used for array indexing and address arithmetic. Be aware that although ptrdiff_t is the result of substracting two pointers, there's no guarantee that it doesn't overflow when doing

Categories : C

What does #QNANO signify or mean in C?
The Q probably means it's a quiet not-a-number. The O might mean overflow. What exact platform and compiler did you use?

Categories : C

Linux on reboot process with PPID 1 do not clean up resources (socket)
You cannot assume any cleanup whatever in the event that your server process is sent a KILL signal (i.e. signal 9). It is usual, however, for a service to provide some means to trigger a clean shutdown. That could be in the form of an appropriate handler for the TERM signal (signal 15), which is desirable to have in any case. Moreover, it is wise to set up your service to be managed via your sy

Categories : C

Why is this implicit conversion (between different pointer types) valid?
When compiling, I get the following warning: temp.c: In function ‘main’: temp.c:20:5: warning: passing argument 1 of ‘f’ from incompatible pointer type [enabled by default] temp.c:13:6: note: expected ‘struct T1 *’ but argument is of type ‘struct T2 *’ It's "valid" because they're both pointers, and hence can be converted, it's just not a good idea.

Categories : C

Exc_Bad_Access in C
The problem is here return k_vector_calculation(&krec[3]); You have passed an int array krek[3] in which the valid locations are krek[0],krek[1] and krek[2].Also,you have an infinite recursion as you call the function again and again in the last return statement.

Categories : C

Converting python string object to c char* using ctypes
Thanks to Eryksun the solution: Python code string1 = "my string 1" string2 = "my string 2" # create byte objects from the strings b_string1 = string1.encode('utf-8') b_string2 = string2.encode('utf-8') # send strings to c function my_c_function.argtypes = [ctypes.c_char_p, ctypes_char_p] my_c_function(b_string1, b_string2)

Categories : C

Why does this struct not align properly?
Usually to minimize paddings, it's normal practice to order the members from biggest to smallest, could you try to reorder them and see what comes out of this? struct ST { long long ll; //8 bytes int i; //4bytes short s; //2 bytes char ch2; //1 byte char ch1; //1 byte }; total is 16 bytes

Categories : C

"invaiid operands to binary *" when assigning to uint* after g_malloc()
error: invalid operands to binary * ^^^^^^ Obviously you're trying to use a unary operator, so what the heck is going on? Well, you forgot the semicolon ; from the end of the previous line...

Categories : C

Calculator with one function
Change ergebnis = calculate(char optr, float opr1, float opr2); to ergebnis = calculate(optr,opr1,opr2); and add float ergebnis; in the calculate function.Also change return1; to return 1;.You also need to place some check to avoid division by zero. You've also used cout instead of printf in the default case.You need to include <stdlib.h> in order to use exit(0);

Categories : C

How to ignore a define directive?
You wrote that preprocessor directives run before the other directives, so nothing is defined at that time. In this case I think the only solution would be to preprocess the file before the preprocessor stage using your own script and change the defined values to something that would suit your needs and would not have impact on your code. I don't think there is any better solution.

Categories : C

Implicit declaration of function is invalid in C99
in your main() the function you called is getk_vector_calculation() [which is not k_vector_calculation()] and which is not declared or defined before the usage. To resolve this, either #include the header file containg the declaration of getk_vector_calculation() in your sorce file. [Considering getk_vector_calculation() is in some other file] or, add a forward declaration of getk_vector_calcu

Categories : C

Read serial port from micaz
Try command: sudo cat /dev/ttyUSB1 Before doing this, check that your Micaz is connected through ttyUSB1. You can check it using command: ls /dev If ttyUSB1 is in the list above, it will work.

Categories : C

vfprintf() throws SegFault - but input is well-defined?
When you use va_arg() "function" on the va_list obtained from va_start(), the va_list is modified, so when passed to another function it is NOT the same as you got at the beginning. You could treat va_list as an pointer, and using va_arg() is sth like increment operator (++). You call your function like this: log("%s gestartet: PID = %d", argv[0], getpid()); But vfprint most likely sees it like

Categories : C

People Counter Using Pic and c
I'm not totally certain how you have this setup, but I think that you'll need to clear a variable after it's read. So your code would need to look more like this: int inOut = 0; // Positive for in negative for out while(1) { if(d1 == 1) { if(d2 == 1) { count += inOut; d1 = 0; d2 = 0; } else { inOut =

Categories : C

CAN BUS Acknowledgment error
This is the default behavior of any CAN bus. You need at least two CAN controllers on the bus to establish communication. If there is just one node, you will get nothing but error frames. For debugging purposes, all CAN controllers have a "loop back" debug feature which allows them to acknowledge and listen to their own frames. You must enable this if you are alone on the bus. Alternatively, if

Categories : C

Drawing multiple rectangles using for loops in C
You don't need more than two loops to do this (the following is untested but should give you the idea): for (i = 1; i <= height ; i++) { for (j = 1; j <= width ; j++) { printf("%c", min4(i - 1, j - 1, height - i, width - j) + 'a'); } } Here, min4 is a function that returns the smallest of its four arguments.

Categories : C

forward declaration of typedefed structs
Use 2 include files: blabla.h #include "light_dependencies.h" typedef struct sBlaBlaFooBar BlaBlaFooBar; blabla_internal.h #include "blabla.h" #include "heavy_dependencies.h" struct sBlaBlaFooBar { ... }; blabla.c #include "blabla_internal.h" /* Here is code which deals with the internals of the struct */ usercode.c #include "blabla.h" /* Here is code which only passes struct pointe

Categories : C

binary search nearest match with last occurrence
n.m.'s 2-search approach will work, and it keeps the optimal time complexity, but it's likely to increase the constant factor by around 2, or by around 1.5 if you begin the second search from where the first search ended. If instead you take an "ordinary" binary search that finds the first instance of lnumber (or, if it doesn't exist, a lower bound), and change it so that the algorithm logically

Categories : C

Pass multiple arguments or just an ID?
int foo_func(struct B *pB_, struct D *pD_) makes the function more general-purpose if it is unnecessary to let foo_func() be aware of the gl_array. If foo_func() is a private function, I think both are ok.

Categories : C

Convert a human readable Date to a GMT date
Here's your problem: The Daylight Saving Time flag (tm_isdst) is greater than zero if Daylight Saving Time is in effect, zero if Daylight Saving Time is not in effect, and less than zero if the information is not available. By setting t.tm_isdst = 0; in your code, you are explicitly specifying that there is no daylight saving in effect. You should have provided a negative value. Reference:

Categories : C

Program to code zigzag matrix vertically
This should work for you: #include <stdio.h> int main() { int rows, columns; int rowCount, columnCount; printf("Please enter rows and columns: >"); scanf("%d %d", &rows, &columns); for(rowCount = 0; rowCount < rows; rowCount++) { for(columnCount = 0; columnCount < columns; columnCount++) { if(columnCount % 2 == 0)

Categories : C

VC++ 2010 error in C struct malloc code
MSVC 2010 does not support the modern dialects of C such as C99 - it is still only C89, so you need to move all variable declarations to the start of your function, e.g. a cleaned up version of your function might look like this: Node *createNode(const char *str) { int strLength = strlen(str); char *data = malloc(strLength + 1); Node *temp = malloc(sizeof(*temp)); strcpy(data, st

Categories : C

Convert array of hex values to corresponding ascii characters
#include <stdio.h> #define A 10 #define B 11 #define C 12 #define D 13 #define E 14 #define F 15 int main(void){ int arr_hex[] = {6,1,6,2,6,5,6,A,7,A}; int size = sizeof(arr_hex)/sizeof(*arr_hex); char arr_ascii[size/2]; int i, j; for(j=i=0; j < size/2; i+=2){ printf("%c", arr_ascii[j++] = arr_hex[i]*16 + arr_hex[i+1]); } printf(" "); return 0; }

Categories : C

Code to run a large countdown timer
It seems way easier to just find out the current system time, and compute the countdown timer's value backwards from that. For instance, say you you want to count down to 2021-05-09. The value of the timer is then always going to be the difference between that time and the current time. As the current time increases, the timer's will decrease. This will be accurate as long as the system clock is

Categories : C


Recently Add
Issues with sorting using doubly linked list
Fatal error while printing a 2 dimensional array
What is the pointer pointing to?
Generating a Sudoku Board in C
reading words/strings from file+length of them - c
Is my interpretation correct?Can I print binary representation for double, as eg:1?
order of evaluation for multiple increment operator on pointer
Looking for algorithm to find n unset bits in a bitset
Pthreads - Increase maximum number of read locks
Write one billion files in one Folder BUT "(No space left on device)" error
Compiler doesn't stop on scanf() for input
Line Counter In C
Reasigning a pointer?
The last character is not printed to a file
How do I empty the SDL event queue?
Tilde operator in C
Data type for holding the count of elements in a data structure
What does #QNANO signify or mean in C?
Linux on reboot process with PPID 1 do not clean up resources (socket)
Why is this implicit conversion (between different pointer types) valid?
Exc_Bad_Access in C
Converting python string object to c char* using ctypes
Why does this struct not align properly?
"invaiid operands to binary *" when assigning to uint* after g_malloc()
Calculator with one function
How to ignore a define directive?
Implicit declaration of function is invalid in C99
Read serial port from micaz
vfprintf() throws SegFault - but input is well-defined?
People Counter Using Pic and c
© Copyright 2017 spot7.org Publishing Limited. All rights reserved.