Search Results

Search found 3887 results on 156 pages for 'pointer arithmetic'.

Page 14/156 | < Previous Page | 10 11 12 13 14 15 16 17 18 19 20 21  | Next Page >

  • Why is address zero used for null pointer?

    - by Joel
    In C (or C++ for that matter), pointers are special if they have the value zero: I am adviced to set pointers to zero after freeing their memory, because it means freeing the pointer again isn't dangerous; when I call malloc it returns a pointer with the value zero if it can't get me memory; I use if (p != 0) all the time to make sure passed pointers are valid etc. But since memory addressing starts at 0, isn't 0 just as a valid address as any other? How can 0 be used for handling null pointers if that is the case? Why isn't a negative number null instead?

    Read the article

  • Freeing a character pointer returns error

    - by Kraffs
    I'm trying to free a character pointer after having used it but it returns a strange error. The error says: "_CrtDbgREport: String too long or IO Error" The debugger itself returns no errors while compiling. The code currently looks like this: void RespondToUser(SOCKET client, SOCKET server) { char buffer[80]; char *temp = malloc(_scprintf("HTTP/1.1 200 OK\r\n%s\r\nServer: %s\r\nConnection: close\r\n\r\nHi!", buffer, SERVER_NAME)); sprintf(temp, "HTTP/1.1 200 OK\r\n%s\r\nServer: %s\r\nConnection: close\r\n\r\nHi!", buffer, SERVER_NAME); send(client, temp, strlen(temp), 0); closesocket(client); free(temp); ListenToUsers(server); } The problem only occurs when I try to free the temp pointer from the memory and not otherwise. What might be causing this?

    Read the article

  • Problem with pointer copy in C

    - by Stefano Salati
    I radically re-edited the question to explain better my application, as the xample I made up wasn't correct in many ways as you pointed out: I have one pointer to char and I want to copy it to another pointer and then add a NULL character at the end (in my real application, the first string is a const, so I cannot jsut modify it, that's why I need to copy it). I have this function, "MLSLSerialWriteBurst" which I have to fill with some code adapt to my microcontroller. tMLError MLSLSerialWriteBurst( unsigned char slaveAddr, unsigned char registerAddr, unsigned short length, const unsigned char *data ) { unsigned char *tmp_data; tmp_data = data; *(tmp_data+length) = NULL; // this function takes a tmp_data which is a char* terminated with a NULL character ('\0') if(EEPageWrite2(slaveAddr,registerAddr,tmp_data)==0) return ML_SUCCESS; else return ML_ERROR; } I see there's a problem here: tha fact that I do not initialize tmp_data, but I cannot know it's length.

    Read the article

  • C pointer initialization and dereferencing, what's wrong here?

    - by randombits
    This should be super simple, but I'm not sure why the compiler is complaining here. #include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) { int *n = 5; printf ("n: %d", *n); exit(0); } Getting the following complaints: foo.c: In function ‘main’: foo.c:6: warning: initialization makes pointer from integer without a cast I just want to print the value that the pointer n references. I'm dereferencing it in the printf() statement and I get a segmentation fault. Compiling this with gcc -o foo foo.c.

    Read the article

  • Null Pointer Exception with an array of bitsets

    - by p0ny
    could someone explain to me why the following results in a Null pointer Exception? And how to set a value for bitarray[0]? BitSet[] bitarray; bitarray= new BitSet[10]; bitarray[0].set(1); Also, why something like this work and not result in a pointer exception? BitSet[] bitarray = new BitSet[10]; BitSet bits = new BitSet(32); bits.set(1); bitarray[0] = bits; Thanks

    Read the article

  • Set argument pointer to point to new memory inside a function (without returning it) IN C

    - by user321605
    Hello, Hopefully my title was descriptive enough to attract the right help. I want to write a function that will return 1 thing, and modify a provided pointer in another. My current function declaration is . . . char * afterURL replaceURLS(char * body) What I want to do is copy all of body's data into a new string, and set body to point to this new data. I then want afterURL to point to a location within the new string. My issue is getting the actual pointer that is passed in to this function to point to the new data. Thanks in advance! Rob

    Read the article

  • Referencing an array to a pointer

    - by james
    I want to refer a pointer to an array by another pointer. Example: void exp() { double var[2]; exp1(&var[0]); printf("\n varvalue is %lf\n",var[0]); } void exp1(double *var) { //updating the value *var[0]=4.0; exp2(&var[0]); } void exp2(double *var) { *var[0]=7.0; } This should update the value as 7.0(the last update).I am getting an array like invalid argument type of unary(*) . How can i correct this?where i am going wrong here?

    Read the article

  • A pointer member variable having different values

    - by Rohan Prabhu
    Ok, to begin with, this is my code: HyperSprite::HyperSprite() { _view = 0; } void HyperSprite::publish(QGraphicsView* view) { _view = view; } void HyperSprite::getKFrame() { if(_view != 0) { qDebug()<<(void*)_view; } } Now, if I call HyperSprite::getKFrame() from within main(), I get the output: 0xbf8ffb84 I have a TCP server, which requires this QGraphicsView* variable. So whenever a new connection is made, HyperSprite::getKFrame() is called. However, whenever I make a connection to my server, this is the output: 0x1e425ff I honestly don't understand this. Shouldn't the value of a member remain same throughout? Why is the pointer value changing? As is obvious, whenever I try to use the _view pointer to access any of its members, a Segmentation Fault occurs. I tried using QSharedPointer, but it also results in the same problem. The data of the QSharedPointer automatically changes. Why is this happening?

    Read the article

  • Reading function pointer syntax

    - by bobobobo
    Everytime I look at a C function pointer, my eyes glaze over. I can't read them. From here, here are 2 examples of function pointer TYPEDEFS: typedef int (*AddFunc)(int,int); typedef void (*FunctionFunc)(); Now I'm used to something like: typedef vector<int> VectorOfInts ; Which I read as typedef vector<int> /* as */ VectorOfInts ; But I can't read the above 2 typedefs. The bracketing and the asterisk placement, it's just not logical. Why is the * beside the word AddFunc..?

    Read the article

  • how to copy an array into somewhere else in the memory by using the pointer

    - by user2758510
    I am completely new in c++ programming. I want to copy the array called distances into where pointer is pointing to and then I want to print out the resul to see if it is worked or not. this is what I have done: int distances[4][6]={{1,0,0,0,1,0},{1,1,0,0,1,1},{1,0,0,0,0,0},{1,1,0,1,0,0}}; int *ptr; ptr = new int[sizeof(distances[0])]; for(int i=0; i<sizeof(distances[0]); i++){ ptr=distances[i]; ptr++; } I do not know how to print out the contents of the pointer to see how it works.

    Read the article

  • pointer and malloc [closed]

    - by gcc
    How many methods/ways are there taking input by using with pointer and dynamic memory? Input: 3 1 2 n k l 2 1 2 p 4 55 62 * # x (x is stop value, first input always integer) Example code: p=malloc(sizeof(int)); scanf("%d",&num_arrays); while(1) { scanf("%c",&(*(p+i))); if(*(p+i)=='x') break; ++i; } "3" is stored in num_arrays. The other input values are stored in pointer[array].

    Read the article

  • How to convert a void pointer to array of classes

    - by user99545
    I am trying to convert a void pointer to an array of classes in a callback function that only supports a void pointer as a means of passing paramaters to the callback. class person { std::string name, age; }; void callback (void *val) { for (int i = 0; i < 9; i++) { std::cout << (person [])val[i].name; } } int main() { person p[10]; callback((void*)p); } My goal is to be able to pass an array of the class person to the callback which then prints out the data such as their name and age. However, the compile does not like what I am doing and complains that error: request for member 'name' in 'val', which is of non-class type 'void*' How can I go about doing this?

    Read the article

  • base pointer to derived class

    - by Jay
    Suppose there are Base class and Derived class. Base *A = new Base; Here A is a pointer point to Base class, and new constructs one that A points to. I also saw Base *B = new Derived; How to explain this? B is a pointer to Base Class, and a Derived class constructed and pointed by B? If there is a function derived from Base class, say, Virtual void f(), and it's been overridden in Derived class, then B->f() will invoke which version of the function? version in Base class, or version that overridden in Derived Class. What if there is a new function void g()in Derived, is B->g() going to invoke this function properly? One more is, is int *a = new double; or int *a = new int; legal?

    Read the article

  • reopen or read and say why not reopened [closed]

    - by gcc
    input 23 3 4 4 42 n 23 0 9 9 n n n 3 9 9 x //according to input,i should create int pointer arrays. pointer arrays // starting from 1 (that is initial arrays is arrays[1].when program sees n ,it // must be jumb to arrays 2 // the first int input 23 is num_arrays which used in malloc(sizeof(int)*num_arrays expected output: elements of arrays[1] 3 4 5 42 elements of arrays[2] 23 0 9 9 elements of arrays[5] 3 9 9 another input 12 2 3 4 n n 2 3 4 n 12 3 x expected output elements of arrays[1] 2 3 4 elements of arrays[3] 2 3 4 elements of arrays[4] 12 3 specification: x is stopper n is comman to create new pointer array i am new in this site anyone help me how can i write

    Read the article

  • Saturated addition of two signed Java 'long' values

    - by finnw
    How can one add two long values (call them x and y) in Java so that if the result overflows then it is clamped to the range Long.MIN_VALUE..Long.MAX_VALUE? For adding ints one can perform the arithmetic in long precision and cast the result back to an int, e.g.: int saturatedAdd(int x, int y) { long sum = (long) x + (long) y; long clampedSum = Math.max((long) Integer.MIN_VALUE, Math.min(sum, (long) Integer.MAX_VALUE)); return (int) clampedSum; } or import com.google.common.primitives.Ints; int saturatedAdd(int x, int y) { long sum = (long) x + (long) y; return Ints.saturatedCast(sum); } but in the case of long there is no larger primitive type that can hold the intermediate (unclamped) sum. Since this is Java, I cannot use inline assembly (in particular SSE's saturated add instructions.) It can be implemented using BigInteger, e.g. static final BigInteger bigMin = BigInteger.valueOf(Long.MIN_VALUE); static final BigInteger bigMax = BigInteger.valueOf(Long.MAX_VALUE); long saturatedAdd(long x, long y) { BigInteger sum = BigInteger.valueOf(x).add(BigInteger.valueOf(y)); return bigMin.max(sum).min(bigMax).longValue(); } however performance is important so this method is not ideal (though useful for testing.) I don't know whether avoiding branching can significantly affect performance in Java. I assume it can, but I would like to benchmark methods both with and without branching. Related: http://stackoverflow.com/questions/121240/saturating-addition-in-c

    Read the article

  • Multiplication algorithm for abritrary precision (bignum) integers.

    - by nn
    Hi, I'm writing a small bignum library for a homework project. I am to implement Karatsuba multiplication, but before that I would like to write a naive multiplication routine. I'm following a guide written by Paul Zimmerman titled "Modern Computer Arithmetic" which is freely available online. On page 4, there is a description of an algorithm titled BasecaseMultiply which performs gradeschool multiplication. I understand step 2, 3, where B^j is a digit shift of 1, j times. But I don't understand step 1 and 3, where we have A*b_j. How is this multiplication meant to be carried out if the bignum multiplication hasn't been defined yet? Would the operation "*" in this algorithm just be the repeated addition method? Here is the parts I have written thus far. I have unit tested them so they appear to be correct for the most part: The structure I use for my bignum is as follows: #define BIGNUM_DIGITS 2048 typedef uint32_t u_hw; // halfword typedef uint64_t u_w; // word typedef struct { unsigned int sign; // 0 or 1 unsigned int n_digits; u_hw digits[BIGNUM_DIGITS]; } bn; Currently available routines: bn *bn_add(bn *a, bn *b); // returns a+b as a newly allocated bn void bn_lshift(bn *b, int d); // shifts d digits to the left, retains sign int bn_cmp(bn *a, bn *b); // returns 1 if a>b, 0 if a=b, -1 if a<b

    Read the article

  • How do you calculate div and mod of floating point numbers?

    - by boost
    In Perl, the % operator seems to assume integers. For instance: sub foo { my $n1 = shift; my $n2 = shift; print "perl's mod=" . $n1 % $n2, "\n"; my $res = $n1 / $n2; my $t = int($res); print "my div=$t", "\n"; $res = $res - $t; $res = $res * $n2; print "my mod=" . $res . "\n\n"; } foo( 3044.952963, 7.1 ); foo( 3044.952963, -7.1 ); foo( -3044.952963, 7.1 ); foo( -3044.952963, -7.1 ); gives perl's mod=6 my div=428 my mod=6.15296300000033 perl's mod=-1 my div=-428 my mod=6.15296300000033 perl's mod=1 my div=-428 my mod=-6.15296300000033 perl's mod=-6 my div=428 my mod=-6.15296300000033 Now as you can see, I've come up with a "solution" already for calculating div and mod. However, what I don't understand is what effect the sign of each argument should have on the result. Wouldn't the div always be positive, being the number of times n2 fits into n1? How's the arithmetic supposed to work in this situation?

    Read the article

  • Bizzare Java invalid Assignment Operator Error

    - by Kay
    public class MaxHeap<T extends Comparable<T>> implements Heap<T>{ private T[] heap; private int lastIndex; private static final int defaultInitialCapacity = 25; public void add(T newItem) throws HeapException{ if (lastIndex < Max_Heap){ heap[lastIndex] = newItem; int place = lastIndex; int parent = (place – 1)/2; //ERROR HERE********** while ( (parent >=0) && (heap[place].compareTo(heap[parent])>0)){ T temp = heap[place]; heap[place] = heap[parent]; heap[parent] = temp; place = parent; parent = (place-1)/2; }else { throw new HeapException(“HeapException: Heap full”); } } } Eclipse complains that there is a: "Syntax error on token "Invalid Character", invalid AssignmentOperator" With the red line beneath the '(place-1)' There shouldn't be an error at all since it's just straight-forward arithmetic. Or is it not that simple?

    Read the article

  • bubble sort on array of c structures not sorting properly

    - by xmpirate
    I have the following program for books record and I want to sort the records on name of book. the code isn't showing any error but it's not sorting all the records. #include "stdio.h" #include "string.h" #define SIZE 5 struct books{ //define struct char name[100],author[100]; int year,copies; }; struct books book1[SIZE],book2[SIZE],*pointer; //define struct vars void sort(struct books *,int); //define sort func main() { int i; char c; for(i=0;i<SIZE;i++) //scanning values { gets(book1[i].name); gets(book1[i].author); scanf("%d%d",&book1[i].year,&book1[i].copies); while((c = getchar()) != '\n' && c != EOF); } pointer=book1; sort(pointer,SIZE); //sort call i=0; //printing values while(i<SIZE) { printf("##########################################################################\n"); printf("Book: %s\nAuthor: %s\nYear of Publication: %d\nNo of Copies: %d\n",book1[i].name,book1[i].author,book1[i].year,book1[i].copies); printf("##########################################################################\n"); i++; } } void sort(struct books *pointer,int n) { int i,j,sorted=0; struct books temp; for(i=0;(i<n-1)&&(sorted==0);i++) //bubble sort on the book name { sorted=1; for(j=0;j<n-i-1;j++) { if(strcmp((*pointer).name,(*(pointer+1)).name)>0) { //copy to temp val strcpy(temp.name,(*pointer).name); strcpy(temp.author,(*pointer).author); temp.year=(*pointer).year; temp.copies=(*pointer).copies; //copy next val strcpy((*pointer).name,(*(pointer+1)).name); strcpy((*pointer).author,(*(pointer+1)).author); (*pointer).year=(*(pointer+1)).year; (*pointer).copies=(*(pointer+1)).copies; //copy back temp val strcpy((*(pointer+1)).name,temp.name); strcpy((*(pointer+1)).author,temp.author); (*(pointer+1)).year=temp.year; (*(pointer+1)).copies=temp.copies; sorted=0; } *pointer++; } } } My Imput The C Programming Language X Y Z 1934 56 Inferno Dan Brown 1993 453 harry Potter and the soccers stone J K Rowling 2012 150 Ruby On Rails jim aurther nil 2004 130 Learn Python Easy Way gmaps4rails 1967 100 And the output ########################################################################## Book: Inferno Author: Dan Brown Year of Publication: 1993 No of Copies: 453 ########################################################################## ########################################################################## Book: The C Programming Language Author: X Y Z Year of Publication: 1934 No of Copies: 56 ########################################################################## ########################################################################## Book: Ruby On Rails Author: jim aurther nil Year of Publication: 2004 No of Copies: 130 ########################################################################## ########################################################################## Book: Learn Python Easy Way Author: gmaps4rails Year of Publication: 1967 No of Copies: 100 ########################################################################## ########################################################################## Book: Author: Year of Publication: 0 No of Copies: 0 ########################################################################## We can see the above sorting is wrong? What I'm I doing wrong?

    Read the article

  • Recommendations for screen-capture tool that retains menus and mouse pointer

    - by Chris Farmer
    There are several screen capture tool questions already on this site, but I think I have a slight tweak to the usual. I'd like a handy screen capture utility that can: Run in Windows 7 Capture a sub-region of the screen Show the current state of the mouse cursor and any active menus I'd like to use it for writing docs that refer to specific application menu items. I like the built-in snipping tool in Windows 7, but the act of using it dismisses my menus, and it doesn't get the mouse cursor either. Do any such tools exist? EDIT: Ahh, I wasn't familiar with the "delayed capture" options of these tools, but they all seem to fit the bill. Thanks!

    Read the article

  • Selective Pointer device remapping in linux

    - by user6368
    I just got an HP 2710p (hp tablet, with digitizer), and I've played around with linux for a while now, and thought I would go ahead and install it. Everything works fine, excepting normal tablet functions, which is to be expected. I'm working on the screen rotation, and there are on-screen keyboards, etc, but I'm having issues with the stylus. I can tap and left click with the stylus as normal, but the side button (which in windows functions as a right mouse button) appears as a 'button 2' to xev (a middle/scroll wheel button). I can switch 'button 2' and 'button 3' universally using xmodmap, but I'd like to do so exclusively for stylus so I don't screw up regular pointing devices. Altering xorg.conf (which is surprisingly bare) with the recommended sections (adding sections for each of the stylus buttons) does nothing. I'm running crunchbang, which is an ubuntu/debian varient with openbox as the windows manager. Thanks Also, as a seperate note, does anybody know how to detect when I rotate and/or latch the lid shut? I was thinking maybe I could run a script to switch the buttons when I close it, but I can't find any information.

    Read the article

  • Toshiba laptop only shows black screen with mouse pointer after starting up

    - by BubblySue
    I only see black screen after the startup. It just shows the logo and the status bar upon start, then it goes black screen with moveable cursor. I tried alt+ctrl+del, but it doesn't work. I pressed shift 5 times and it makes a sound. I already removed battery and restarted it, but still the same. I can go to safe mode and scanned thru there. Still, desktop won't show up. Don't know what else to check?

    Read the article

  • Toshiba laptop only shows black screen with mouse pointer after starting up

    - by BubblySue
    I only see black screen after the startup. It just shows the logo and the status bar upon start, then it goes black screen with moveable cursor. I tried alt+ctrl+del, but it doesn't work. I pressed shift 5 times and it makes a sound. I already removed battery and restarted it, but still the same. I can go to safe mode and scanned thru there. Still, desktop won't show up. Don't know what else to check?

    Read the article

< Previous Page | 10 11 12 13 14 15 16 17 18 19 20 21  | Next Page >