c

Allocate room for null terminating character when copying strings in C?

Hello, so say I have this: const char* src = "hello"; Calling strlen(src); returns size 5... Now say I do this: char* dest = new char[strlen(src)]; strcpy(dest, src); That doesn't seem like it should work, but when I output everything it looks right. It seems like I'm not allocating space for the null terminator on the end... i...

How to write an evaluator for a string like "(1+3 * ( 5 / 4)) and get a numeric result.

hi, this is a interview question i am confused about its solution, i am thinking that i need stacks to push and pop these operators and operands, but do i need two stacks, one for operator and one for operands? or would just one stack do?i think we need two stacks but is there any way to solve using one stack? also i am bit confused ho...

l2cap server/client using IOBluetooth (osx bluetooth stack)

I'm having trouble understanding the API to set up a l2cap (or RFCOMM) client/server running on OSX like I can with BlueZ on Linux. On Linux, I simply open a socket, bind, listen & then accept for the server, & socket, bind, connect for the client (w/ the bind taking in the BT address of the device I want to use). Also, there's no pair...

What does out[i] = *(a_mat + i) do in C?

while( i < a_rows * a_cols ) { out[i] = *(a_mat + i); // this line i++; } What does the marked line do? ...

what's the mechanism of sizeof() in C/C++?

It seems sizeof is not a real function? for example, if you write like this: int i=0; printf("%d\n", sizeof(++i)); printf("%d\n", i); You may get output like: 4 0 And when you dig into the assemble code, you'll find sth like this: movl $4, %esi leaq LC0(%rip), %rdi xorl %eax, %eax call _printf So, the compiler put d...

Using strtok() on an allocated string?

Is there anything I should know about using strtok on a malloced string? In my code I have (in general terms) char* line=getline(); Parse(dest,line); free(line); where getline() is a function that returns a char * to some malloced memory. and Parse(dest, line) is a function that does parsing online, storing the results in dest, (whic...

Noob-Ready Cython Tutorials

I know a bunch of scripting languages, (python, ruby, lua, php) but I don't know any compiled languages like C/C++ , I wanted to try and speed up some python code using cython, which is essentially a python -> C compiler, aimed at creating C extensions for python. Basically you code in a stricter version of python which compiles into C -...

Simple C array declaration / assignment question

In higher level languages I would be able something similar to this example in C and it would be fine. However, when I compile this C example it complains bitterly. How can I assign new arrays to the array I declared? int values[3]; if(1) values = {1,2,3}; printf("%i", values[0]); Thanks. ...

Best practice for the Python-then-profile-then-C design pattern?

Hi all. A popular software development pattern seems to be: Thrash out the logic and algorithms in Python. Profile to find out where the slow bits are. Replace those with C. Ship code that is the best trade-off between high-level and speedy. I say popular simply because I've seen people talk about it as being a great idea. But are t...

Linux Tool to read values of the stack

Hello, I would like to know if there is a Linux tool that allows you to read the values of the program stack?? For instance when running the binary of a program containing the line: foo(parameter); the parameter would be put on the stack, and I would like to know if there is a tool to access it. thanks. ...

Making a C extension to Python that requires another extension

I have a couple of Python functions that I use to make game development with Pygame easier. I have them in a file called helper.py in my Python-path, so I can import them from any game I make. I thought, as an exercise to learn about Python extensions, to convert this module to C. My first problem is that I need to use functions from Pyg...

Using SWIG with pointer to function in C struct

I'm trying to write a SWIG wrapper for a C library that uses pointers to functions in its structs. I can't figure out how to handle structs that contain function pointers. A simplified example follows. test.i: /* test.i */ %module test %{ typedef struct { int (*my_func)(int); } test_struct; int add1(int n) { return n+1; } tes...

Find a prime number?

To find whether N is a prime number we only need to look for all numbers less or equal to sqrt(N). Why is that? I am writing a C code so trying to understand a reason behind it. ...

malloc code in C

I have a code block that seems to be the code behind malloc. But as I go through the code, I get the feeling that parts of the code are missing. Does anyone know if there is a part of the function that's missing? Does malloc always combine adjacent chunks together? int heap[10000]; void* malloc(int size) { int sz = (size + 3) / 4; int ...

casting issue with realpath function (c programming)

When I compile the following code: #define _POSIX_C_SOURCE 200112L #define _ISOC99_SOURCE #define __EXTENSIONS__ #include <stdio.h> #include <limits.h> #include <stdlib.h> int main(int argc, char *argv[]) { char *symlinkpath = argv[1]; char actualpath [PATH_MAX]; char *ptr; ptr = realpath(symlinkpath, actualpath); ...

Populating Heap (malloc) with a string array

hello, i'm trying to populate the heap with a string array, but the console gave me nothing when i compiled. I don't know what I did wrong... void spellCheck(char article[], char dictionary[]) { int i = 0; char* tempArticle; while ( article[i] != '\0'){ i++; } tempArticle = malloc(i); i=0; while (article...

Convert every letter from a paragraph (string) to lowercase before storing it back to the string (C Programming)

I'm trying to use the following code to convert every letters from a paragraph (strings) into lowercase letters before storing it back to the string. But the compiler wouldn't compile =( #include <stdio.h> #include <stdlib.h> #include <string.h> void spellCheck(char article[], char dictionary[]) { int i = 0; char* tempArticle; ...

Most optimal way to find the sum of 2 numbers represented as linked lists

Hello, I was trying to write a program for the problem I mentioned above, the numbers (i.e the lists) can be of unequal length, I was not able to figure out a way to do this other than the most commonly thought of approach i.e reverse list-1 reverse list-2 find the sum and store it in a new list represented by list-3 reverse the li...

add current/selected user to a group

Hi all can anyone tell me how can I (programatically) add the current/selected user to a group (like power user, backup opetarors) any function/info/code is welcomed ...

unsigned char array to unsigned int back to unsigned char array via memcpy is reversed

This isn't cross-platform code... everything is being performed on the same platform (i.e. endianess is the same.. little endian). I have this code: unsigned char array[4] = {'t', 'e', 's', 't'}; unsigned int out = ((array[0]<<24)|(array[1]<<16)|(array[2]<<8)|(array[3])); std::cout << out << std::endl; unsigned char b...