gerard4143 371 Nearly a Posting Maven

please help me...Yet I ve not found the answer any where , I am very tensed from this question

A character variable largest integer value is 127. Once it reaches 127(the character variable) the value will wrap around to the lowest values which is -128. So do you see? A character variable can't be larger than 127.

gerard4143 371 Nearly a Posting Maven

Your using a character pointer that hasn't been initialized. Line 8 should be

char* pStr = (char*)malloc(200 * sizeof(char));
/*check allocation here*/
gerard4143 371 Nearly a Posting Maven

Here's a question. How big can a character variable get?

gerard4143 371 Nearly a Posting Maven

Not really sure what you program's purpose is, are you trying for something like below?

#include <stdio.h>

int main() 
{
  int i = 0, size = 0;

  printf("Enter the size of the arrays->");
  scanf("%d", &size);
  
  int array1[size];
  
  for (i = 0; i < size; ++i)
  {
    fprintf(stdout, "Enter element[%d]->", i);
    fscanf(stdin, "%d", &array1[i]);
  }

  for (i = 0; i < size; ++i)
    fprintf(stdout, "element[%d]->%d\n", i, array1[i]);

  return 0;
}
gerard4143 371 Nearly a Posting Maven

Look at line 6. Your incrementing a[1] which equals 3 by 1 so i = 4.
Look at line 8. Your incrementing i so that would make i = 5.

gerard4143 371 Nearly a Posting Maven

I have the wikibooks version of "The C programming language", but I did not manage to figure this one out. I am actually learning C++, and I have used C before only in the context of embedded systems, so I havent had any need for the use of malloc before.

In my C++ primer, I have the following text:

string s ("some value");

string *sp = &s;       //sp holds the adress of s
cout << *sp;           //prints some value

*sp = "a new value";   //contents of s now changed

So assigning a string directly to the dereferenced pointer doesnt work when using malloc, ok.

Anyway, thanks alot!

The problem with your original code is, your allocating memory and assigning the pointer to the allocated memory to the pointers p1 and p2 and then you promptly assign the string literals "Clydefrog" and "Cartman" to the pointers p1 and p2...which creates a memory leak.

gerard4143 371 Nearly a Posting Maven

You define

struct bst root;

and here your allocating memory for it

root=(struct bst*)malloc(sizeof(struct bst));

Why?

gerard4143 371 Nearly a Posting Maven

Look at line 15.

void del_singlechild():

Your using a : and not a ;

gerard4143 371 Nearly a Posting Maven

Are you talking about a console program? If you are then you'll have to state which operating system.

gerard4143 371 Nearly a Posting Maven

but ultimately it isn't worth the effort for a weak facsimile of function overloading.

Wow you got function overloading from the original posting? You must be psychic.

gerard4143 371 Nearly a Posting Maven

If your not using the unsafe keyword, I doubt you have a memory leak or a memory leak that you cam fix(i.e. It may be a problem with the database api).

Note: I wouldn't base memory leaks solely on the task manager's memory indicator since C#'s garage collector isn't consistent in its duties.

gerard4143 371 Nearly a Posting Maven

The C Programming Language by Brian W. Kernighan, Dennis Ritchie

gerard4143 371 Nearly a Posting Maven

Just curious. What C compile allows main to have no return type?

gerard4143 371 Nearly a Posting Maven

Try the code below

#include<stdio.h>

int main()
{
  char name[30];
  
  
  printf("enter name:");
  fgets(name,50,stdin);
  
  printf(name);

  return 0;
}

The code you posted worked, it just got the name from the file. This one gets it from the stdin(keyboard).

gerard4143 371 Nearly a Posting Maven

How do you know it didn't work?

gerard4143 371 Nearly a Posting Maven

I just tried compiling your program. I got four pages of errors/warnings.

From little obvious things like

void hammers(string bagofhammers);//Note the semi-colon. It shouldn't be there.
{
setDescription = bag of hammers;
}
void hammers(int);//Note the semi-colon. It shouldn't be there.
{
setQuantity = 1;
}
void hammers (int);//Note the semi-colon. It shouldn't be there.
{
setPrice = 12.99;
}
gerard4143 371 Nearly a Posting Maven

The last part of your posting

void print()
{
cout <<"Invoiced item is: " << getDescription << endl;

cout <<"Quantity ordered: "<< getQuantity << endl;

cout <<"Each unit's price is: "<< getPrice << endl;

cout <<"Total Amount: "<< (setPrice*setQuantity) << endl;
};



}

Should probably be

void print()
{
cout <<"Invoiced item is: " << getDescription << endl;

cout <<"Quantity ordered: "<< getQuantity << endl;

cout <<"Each unit's price is: "<< getPrice << endl;

cout <<"Total Amount: "<< (setPrice*setQuantity) << endl;
}



};

Note line 14.

gerard4143 371 Nearly a Posting Maven

Instead of posting your homework assignment(and breaking forums rules) why don't you repost your code correctly and state what's not working and where its not working.

gerard4143 371 Nearly a Posting Maven

Before I spent anymore time on this question, could you please state what line number you are talking about.

gerard4143 371 Nearly a Posting Maven
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


void heapstrings(void)
{


    char *p1 = (char*)malloc(sizeof(char[13]));
    char *p2 = (char*)malloc(sizeof(char[13]));

//check here to see if malloc failed.

    strcpy(p1, "Clydefrog");
    strcpy(p2, "Cartman");


    printf("p1 is pointing on string %s at adress %p\n", p1, (void*)p1);
    printf("p2 is pointing on string %s at adress %p\n", p2, (void*)p2);

}


int main()
{
    heapstrings();
    printf("Hello world!\n");
    return 0;
}

Get yourself a decent book on C programming - The C Programming Language by Brian W. Kernighan, Dennis Ritchie, comes to mind.

gerard4143 371 Nearly a Posting Maven

This is my header file i want to use

#include "my.h"
#include <stdio.h>

void read_data(int* length, int* width, int* customerdiscount, double* costpersquarefoot)
{
printf("Length of room (feet)?");
scanf("%d\n", *length);//should be scanf("%d\n", length);

printf("Width of room (feet)?");
scanf("%d\n", *width);//should be scanf("%d\n", width);

printf("Customer discount (percent)?");
scanf("%d\n", *customerdiscount);//should be scanf("%d\n", customerdiscount);
printf("Cost per square foot (xxx.xx)?");
scanf("%6.2f\n", *costpersquarefoot);//should be scanf("%6.2f\n", costpersquarefoot);

 return;
}

You should read up on how scanf works.

gerard4143 371 Nearly a Posting Maven

I'm not sure if the gcc compiler has that option. The ld(GNU linker) can change the entry point label but its default is _start and it won't apply to your code.

ld

-e entry
--entry=entry
Use entry as the explicit symbol for beginning execution of your
program, rather than the default entry point. If there is no
symbol named entry, the linker will try to parse entry as a
number, and use that as the entry address (the number will be
interpreted in base 10; you may use a leading 0x for base 16, or
a leading 0 for base 8).

gerard4143 371 Nearly a Posting Maven

Well we're not writing the code for you. What have you written so far?

To calculate even or odd numbers you would use the modulus operator and for your loop, you could use either a while or a for loop.

gerard4143 371 Nearly a Posting Maven

Not sure what your trying to accomplish in your code but you could investigate the rename function that resides in cstdio or stdio.h.

int rename(const char *oldpath, const char *newpath);
gerard4143 371 Nearly a Posting Maven

I think this is what you want

#include <iostream>

class m1
{
public:
  
  virtual void display_it(void) const { std::cout << "from m1" << std::endl; }
  
};

class m2:public m1
{
public:
  
  void display_it(void) const { std::cout << "from m2" << std::endl; }
  
};

int main(int argc, char** argv)
{
    m2 me;
    
    me.m1::display_it();
    return 0;
}
gerard4143 371 Nearly a Posting Maven

Thank-you for the replies. I understand passing by value(both values and references), my problem lies in passing by reference. I really don't have an intuitive understanding of the mechanism, especially when we use 'pass by ref' in a deeply nested function call.

I can understand a simple pass by reference, passing a reference(by possibly using a pointer). I can't understand how a more general case would work...A case like the above code. Does the compiler keep generating deeper references or does it generate a reference for the function call and keep incrementing that one reference for each nested call...I hope this makes sense.

gerard4143 371 Nearly a Posting Maven

Try creating a == operator, something like below

bool operator ==(const Sample & s) { return a == s.a; }
gerard4143 371 Nearly a Posting Maven

I'm really new to C# so this may seem like a novice question..How does C# accomplished passing a reference to a object reference? I'm really trying to understand the underlying mechanisms and how it applies to the attached code with the comments - //what happens here? Do we pass a ref->ref->ref....or is this accomplished via reference counting.

using System;

class my_int{
	int itsx;
	int itsy;
	
	public my_int(int x, int y){
		itsx = x;
		itsy = y;
	}
	
	public void display_it(){
		Console.WriteLine("x->{0}, y->{1}", itsx, itsy);
	}
}

namespace testit
{
	class MainClass{
		
		static void myfunc(ref my_int m, int x){
			if ( x < 10 )
				myfunc(ref m, ++x);//what happens here? Do we pass a ref->ref->ref....or is this accomplished via reference counting.
			else
				m = new my_int(888, 999);
		}
		
		static void Main(string [] args){
			my_int me = new my_int(123, 456);//create a reference 'me' to a my_int object
			
			me.display_it();
			
			myfunc(ref me, 0);//pass a reference to the reference 'me'.
			
			me.display_it();
		}
	}
}
gerard4143 371 Nearly a Posting Maven
int main(void)
{
 int i=0;
    printf("%d",++i + ++i + ++i);
}

This code perfectly produces 6 when compiled with Tiny C Compiler V0.9.25.

This is what my compiler say about your code...

warning: operation on ‘i’ may be undefined
warning: operation on ‘i’ may be undefined

Plus it displays 7 on my machine and not 6.

gerard4143 371 Nearly a Posting Maven

Try using fgets to read from stdin.

char *fgets(char *s, int size, FILE *stream)

gerard4143 371 Nearly a Posting Maven

Can we see the code?

gerard4143 371 Nearly a Posting Maven

I herd that some C++ compilers can use asembly languige in there programs along side C++, How would I do This? And is there any way to use a compiler as an assembler? <<two questions.

How do you integrate assembly into C++? Generally most compilers provide inline assembly as an extension to the C++ compiler.

How is this accomplished? Most vendors provide a keyword asm or __asm or __asm__ which you can follow with assembly instructions...Here's a link.

http://msdn.microsoft.com/en-us/library/45yd4tzz%28v=vs.71%29.aspx

Is there a way to use the C++ compiler as an assembler? Yes, all C/C++ compilers I used assembled the code.

gerard4143 371 Nearly a Posting Maven

You should be able to use iterators.

gerard4143 371 Nearly a Posting Maven

Maybe they mistakenly locked your HDD when checking it. I'm not really sure how that happen as well but if try to format fails, might as well check the HDD for bad sector...

Now I was suspecting maybe they locked the hard drive to prevent any data loss from diagnostics but I'm not even sure if you can make a hard drive read-only. Can you set a hard drive to read-only?

gerard4143 371 Nearly a Posting Maven

Hi,

I have an odd problem...well an annoying problem. My internal hard drive is read only which is O.K. unless you need to write to it or say boot it.

So what happened? I really don't know, I sent my computer to get some maintenance done on it because it won't boot, won't power up, it won't even go so far as post any POST errors. It was dead...Wait a minute, I think the power supply fan worked anyways the Acer maintenance people said I had a bad RAM module and they replaced it and said the computer is now fine. Well its fine except now the internal hard drive is read only? I tried accessing the drive via PartedMagic but couldn't delete/format or write to the hard drive(I could read the hard drive with PartedMagic). What can cause a hard drive to be read only?

gerard4143 371 Nearly a Posting Maven

Lets say I have a shared library in Linux, written in C, with a single function that simply returns the pointer to a string literal.

void *ReturnObject() {
  return (void *)"\xFA\x03\x44\x10\xE0";
}

When this library is loaded by the host application at runtime and this function is called, is the returned pointer pointing to some place in RAM or to the binary file on disk? Is RAM ever involved in this situation other than storing the pointer itself?

I ask because I plan on implementing this sort of thing in my project and the string will be several megabytes in size, and I'm worried that this strategy will in some way consume an equal amount of RAM.

If it does eat up RAM, would closing the library connection after the host program is done with this function release that memory?

Linux provides several IPC methods, shared memory and memory queues come to mind.

gerard4143 371 Nearly a Posting Maven

Only if he/she likes a pointer that refers to invalid memory(int 'a' becomes invalid when the function 'check' returns).

dellat commented: Right +0
gerard4143 371 Nearly a Posting Maven

Think of a void pointer as a dimensionless pointer. It has to be cast to a data type with a dimension before it can be referenced...Like below.

int x = 5;
void *vptr = (void*)&x;
int *iptr = (int*)vptr;
fprintf(stdout, "*iptr->%d\n", *iptr);
gerard4143 371 Nearly a Posting Maven

Could you explain what's not working so we can understand what the problem is.

gerard4143 371 Nearly a Posting Maven

Just compiled and ran your code using gcc, it worked fine.

gerard4143 371 Nearly a Posting Maven

Hmm... Your code I copied/pasted and it still didn't work. On line 7 of your code,I get the error 'undefined reference to sum'.

How did you compile it?

gerard4143 371 Nearly a Posting Maven

@cse.avinash
actually i didnt get the meaning of *((char*)iPtr)..what does it mean??

It means cast iPtr to a character pointer with (char*) and then dereference it.

*((char*)iPtr) getting the value it points to.

gerard4143 371 Nearly a Posting Maven

Hello

I'm looking for a javascript I can use to rotate my 10 images on 10 seconds interval.

Wow your a long way from home...Try posting under the Web Development section.

gerard4143 371 Nearly a Posting Maven

Here's an example that works on my AMD/64 bit machine.

testit.cpp

#include <iostream>

extern "C" int sum(int, int);//to avoid name mangling

int main()
{
	std::cout << sum(3, 4) << std::endl;
	return 0;
}

asm_file.s

.section .data

.section .bss

.section .text
	.global sum
sum:
	pushq	%rbp
	movq	%rsp, %rbp

	xorq	%rax, %rax
	addl	%edi, %eax
	addl	%esi, %eax

	movq	%rbp, %rsp
	popq	%rbp
	ret

objdump -D testit.o>testfile

testfile

testit.o:     file format elf64-x86-64


Disassembly of section .text:

0000000000000000 <main>:
   0:	55                   	push   %rbp
   1:	48 89 e5             	mov    %rsp,%rbp
   4:	be 04 00 00 00       	mov    $0x4,%esi //the values and registers we want
   9:	bf 03 00 00 00       	mov    $0x3,%edi //the values and registers we want
   e:	e8 00 00 00 00       	callq  13 <main+0x13>
  13:	89 c6                	mov    %eax,%esi
  15:	bf 00 00 00 00       	mov    $0x0,%edi
  1a:	e8 00 00 00 00       	callq  1f <main+0x1f>
  1f:	be 00 00 00 00       	mov    $0x0,%esi
  24:	48 89 c7             	mov    %rax,%rdi
  27:	e8 00 00 00 00       	callq  2c <main+0x2c>
  2c:	b8 00 00 00 00       	mov    $0x0,%eax
  31:	c9                   	leaveq 
  32:	c3                   	retq

And the compile lines.

g++ testit.cpp -c
as asm_file.s -o asm_file.o
g++ asm_file.o testit.o -o testit

gerard4143 371 Nearly a Posting Maven

Try this

#include <stdio.h>

int main(void)
{
    printf("%lu %lu",sizeof((unsigned char)'A'),sizeof("A"));
    return 0;
}

A character literal has type int.

gerard4143 371 Nearly a Posting Maven

Maybe it would help to look at what 258 is in binary

00000001 00000010

Which is 1 and 2.

gerard4143 371 Nearly a Posting Maven

I would multiply both your example numbers(0.60 and 0.69) by 100 and go from there.

You might find some help here..

http://www.daniweb.com/software-development/c/threads/346402

gerard4143 371 Nearly a Posting Maven

Typing is good, it allows the compiler to enforce its type restriction. That's good by the way.

gerard4143 371 Nearly a Posting Maven

Unions is a poor man's way to express the idea of polymorphism. See for example a definition of X Windows event and related API.

Wow what a poor example. How is a union a poor man's way to express polymorphism? Your link doesn't work.

gerard4143 371 Nearly a Posting Maven

I googled c++ string and got this

http://www.cplusplus.com/reference/string/string/