I have installed the gcc compiler from this sudo apt-get install build-essential command
and my program code is
#include<stdio.h>
main()
{
int *b;
b = (int*)malloc(10*sizeof(int));
printf("b=%u\n\n",b);
printf("b+1=%u\n\n",(b+1));
printf("b+2=%u\n\n",(b+2));
b[2]=4;
printf("*(b+2)=%d\n\n",*(b+2));
}
when i try to compile this program from cc -c program.c command
then i get some error
You're missing #include <stdlib.h> (for malloc), and the format strings are wrong. Use %p to print pointers.
Also, you don't need to (and probably shouldn't) cast the return value of malloc (in C).
And the correct signature for main without parameters is:
int main(void)
Corrected code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *b;
b = (int*)malloc(10*sizeof(int));
printf("b=%p\n\n", (void*) b);
printf("b+1=%p\n\n",(void*) (b+1));
printf("b+2=%p\n\n",(void*) (b+2));
b[2]=4;
printf("*(b+2)=%d\n\n",*(b+2));
return 0;
}
I don't know why it worked in the video, it's probably using some strange non-standard compiler.
But your errors are because you are using int instead of unsigned int and you pass pointers to printf when it expects unsigned int.
Related
I am playing with openssl in C and getting some warnings I like to get rid of and am unsure how to tackle it as the examples I had found seem to do what I am emulating.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/sha.h>
int main(int argc, char** argv)
{
int i;
unsigned char resSHA512[SHA512_DIGEST_LENGTH];
unsigned char unhashed[1024];
printf("Input string to hash: ");
fgets(unhashed, sizeof(unhashed), stdin);
unhashed[strcspn(unhashed, "\n")] = 0;
SHA512(unhashed, strlen(unhashed), resSHA512);
printf("\nSHA512: ");
for ( i = 0; i < SHA512_DIGEST_LENGTH; i++ )
{
printf("%02x", resSHA512[i]);
}
printf("\n");
return 0;
}
I also got an alert that SHA512 has been deprecated by open when I compile on my Mac using openssl I got through it's git repository. I get no warnings in Linux unless I used the -Wall flag with gcc.
Any help is greatly appreciated, as I am trying to learn new things!
Thank you
What you're reading in to unhashed is a string, so you should declare it as an array of char. That will address the warning with the string-related functions.
That leaves you with a warning when calling SHA512 which expects an unsigned char for its first argument. In this case you can safely cast the argument to unsigned char *.
The warning you see on Mac is because the entire OpenSSL library was marked deprecated on that OS in favor of its own crypto library.
I am trying the following, with gcc on win32.
#include <stdio.h>
struct st { char c; int x; } __attribute__ ((packed));
int main() {
printf("%d\n", sizeof(struct st));
return 0;
}
I would expect that the printed value is 5, but it's 8.
With the following, however, I get 5.
#include <stdio.h>
#pragma pack(1)
struct st { char c; int x; };
int main() {
printf("%d\n", sizeof(struct st));
return 0;
}
There must be something wrong in my program, but I can't see what.
I have read gcc's manual and several questions on SO about this, and I'm still puzzled. Any hint?
Also from the answers to these questions on SO, I understand that I should not use packed structs for marshalling, and I probably won't use it much, but I still would like to understand what I'm not able to see in such a short program.
Note: the problem occurs with both gcc-4.9.2 and gcc-4.8.4.
You have the attribute in the wrong place - try this:
struct st { char c;
int x __attribute__ ((packed));
};
As per the example in the gcc manual, this will cause x to be packed such that it immediately follows c.
Of course you shouldn't really be doing this in the first place, as your code will break on certain architectures, and even where it doesn't break there may be performance penalties.
Working fine on my environment Centos 5.11 (64bit)
prints 5 for the first case you mentioned.
gcc version 4.9.1 (GCC)
gcc file.c
./a.out
5
This question already has answers here:
Can anybody tell me why this is happening in c language
(3 answers)
Closed 5 years ago.
#include <stdio.h>
//Compiler version gcc 6.3.0
int main(void)
{
int a=10;
printf("%d"+1,a);
return 0;
}
Output. --- d
#include <stdio.h>
//Compiler version gcc 6.3.0
int main(void)
{
#include <stdio.h>
//Compiler version gcc 6.3.0
int main(void)
{
int a=10;
printf("%d"+36,a);
return 0;
}
Output. --- p
Explain me why this happening... Whenever i change value of +1 it print different outputs...
The behaviour of your code is undefined.
"%d"+1 actually is adding 1 to a const char* pointer (the literal "%d" will decay to a const char* pointer under certain circumstances), which actually takes you to the d in that string literal! The printf formatter is therefore not appropriate for your arguments.
"%d"+36 is simply going to do very bad things indeed, since you don't own the memory 36 places on from the start of "%d"
Don't you mean something like printf("%d", a + 1); &c.?
Lastly, what the deuce is an Int? Didn't you mean int?
I encounter a problem of overflow when use memchr() on mac os x.
Here is my test code:
#include <stdio.h>
#include <stdlib.h>
int main(void){
char *content="http\r\nUser";
int content_size = strlen(content);
char *contmem = malloc(content_size+1);
memset(contmem, '\0', content_size+1);
memcpy(contmem, content, content_size);
printf("%c\n", *(content+content_size));
printf("%c\n", *(contmem+content_size));
char *t = memchr(content, 't', content_size);
printf("%c\n", *t);
return 0;
}
It works normally on linux, i.e., my fedora 16, and prints the correct value of t.
But when I run the same piece of code on Mac, Segmentation Fault occurs!!
After debugging with gdb, I take the saying:
(gdb) print t
$7 = 0xf4b <Address 0xf4b out of bounds>
Then I try to rewrite the memchr function in this test file:
static char*
memchr(const char *data, int c, unsigned long len){
char *tp = data;
unsigned long i;
for( i = 0; i<len; i++){
if((int)*tp == c){
return tp;
}else{
tp = tp+1;
}
}
}
And the output seems correct!
(gdb) print t
$1 = 0x100000f1d "ttp\r\nUser"
So I am confused with the abnormal behavior of memchr() on mac os, while other mem functions like memset() memcpy() works fine.
How can I run the test without rewriting the memchr() on mac??
Thanks.
The function memchr() is declared in string.h, for which there is no include directive in the posted code. This means an implicit function declaration will be generated by compiler (which should emit a warning) which returns an int. If the sizeof(int) and sizeof(char*) are different on your system this may explain the problem. Add:
#include <string.h>
Your code should indeed work. Your compiler may be using built-in versions of the mem***() functions. Try to include string.h to force the use of the libc versions.
Why am I getting this message? The compiler is clang. Here is a simple program where it occurs for examples sake:
#include<stdio.h>
int fib(int);
int main()
{
int i;
scanf("%d",&i);
printf("The fibonacci number that is %i'th in the sequence is %i \n", i, fib(i));
return 0;
}
int fib(int n)
{
if (n==1 || n==0) return 1;
else return fib(n-1)+fib(n-2);
}
Assuming C
<stdio.h> is one of the standard C headers. Your compiler complains that it can not find this header. This means that your standard library is broken.
Consider reinstalling your compiler.
Assuming C++
<stdio.h> is the C standard header, with C++ we use <cstdio> instead. Though <stdio.h> is still required to exist in C++, so this probably isn't the problem.
Apart from these assumptions, it seems most likely (by your coding style and tags) that you are using C. Try this as some example code. This is guaranteed (by me) to compile on a working C compiler, if it doesn't then your compiler is horribly broken and you must install another one/reinstall:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv) {
printf("Hello World!\n");
return EXIT_SUCCESS;
}
I got a runtime error similar to this while using C++ in Visual Studio 2017. The solution in my case was to simply clean and rebuild the solution