This is my code:
#include <stdio.h>
#include <string.h>
int main() {
char x[256];
strcpy(x, "Gonzo!");
printf(strlen(x));
}
I'm not sure that I am coding this correctly. I want to know the string length of x.
I am using XCode 5+ and I get the error:
EXC_BAD_ACCESS(code=1, address=0x6)
The first argument of printf() is a const char*, which specifies the format. The posted code passes in 6 as the starting memory location for the char* which is incorrect and is causing the error. Use:
printf("%d\n", strlen(x));
See the linked printf() reference documentation.
Related
I'm trying to acquaint myself with the atoi function, so I wrote a basic programme using it, but I'm having some problems:
#include <stdio.h>
#include <cs50.h>
#include <stdlib.h>
int main(void)
{
string s = get_string("String:");
int i = get_int("Integer:");
int a = atoi(s[1]);
int j = i + a;
printf("%i\n", j);
}
When I try to compile it, I get the error message "incompatible integer to pointer conversion passing 'char' to parameter of type 'const char *'; take the address with & [-Werror,-Wint-conversion]". This seems to suggest that it wants something to do with a char, but from what I've read, I was under the impression that atoi was used with strings. If someone could explain where I'm going wrong, I'd be very thankful
You're passing a char(assuming string is a typedef for char*) by indexing the string and it wants you to pass a char*. So just pass the full string:
#include <stdio.h>
#include <cs50.h>
#include <stdlib.h>
int main(void)
{
string s = get_string("String:");
int i = get_int("Integer:");
int a = atoi(s);
int j = i + a;
printf("%i\n", j);
}
here is the syntax for atoi()
int atoi(const char *nptr);
Notice the parameter is a pointer, not a single character.
However, the posted code has:
int a = atoi(s[1]);
which is a single character, not a pointer
suggest:
int a = atoi( s );
Also, the function: atoi() has no facility to let the program know when an error occurred. Suggest using the strtol() function which does have a facility to indicate when an error occurred
I m Writing program for dynamic memory allocation. In this program i getting error of undefined symbol _msize. I have also include . Please help me with this.
/* Example of _msize */
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
void main()
{
long *buffer;
size_t size;
buffer = (long *)malloc(100 * sizeof(long));
size = _msize(buffer);
printf("The size of the buffer is %d\n", size);
}
The _msize is not Standard C.
It is from Microsoft Visual C++(MSVC), and you need to include <malloc.h>, as you already did, and use a Microsoft Compiler (e.g. within the Visual Studio).
PS: Unrelated to your problem: What should main() return in C and C++?
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 looked around, and found a program where swprintf is used from the <wchar.h> library. However i tried that method, and it didn't work. It just says program.exe has stopped working when i run the program. I copied the sample program i found, into my own compiler-- which didn't work either. I'll copy the program below:
#include <stdio.h>
#include <wchar.h>
int main(void)
{
wchar_t NameBuffer[100];
char *str1 ="c:\\Program Files\\test.txt";
swprintf(NameBuffer,100,L"%s",str1);
printf("%s\n",NameBuffer);
return 0;
}
Is there something wrong with this code or is it my compiler?
Try converting with mbstowcs():
#include <stdio.h>
#include <wchar.h>
#include <stdlib.h>
int main(void)
{
wchar_t NameBuffer[100];
char *str1 ="c:\\Program Files\\test.txt";
mbstowcs(NameBuffer, str1, 100);
wprintf(L"%ls\n",NameBuffer);
return 0;
}
In the code above, in the printf line
printf("%s\n",NameBuffer);
NameBuffer is not a (converted) value of type char * and therefore you invoke Undefined Behaviour by not matching the %s and its corresponding argument.
Try
wprintf(L"%ls\n", NameBuffer);
or
printf("%ls\n", NameBuffer);
You may also want to define str1 as pointer to const
const char *str1 = "c:\\Program Files\\test.txt";
I finished my programming classes in C, and thought I would write some code down. BOOM! I run into so many problems. I guess the C language is so complicated, even a book can't explain how it works entirely.
This is my problem(I am trying to display something using a pointer)
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
void main()
{
char *a;
system("cls");
*a = {"hello"};
printf("%s",a);
getch();
}
*a = {"hello"};
This dereferenced the pointer and assigned something to that memory location. The reason it crashed is because the pointer was uninitialised, ie it did not point at anything (actually it did point at something, but that something was an undefined memory location).
If you had done the following, your program would have worked:
a = "hello";
The type of a is char*. The type of "hello" is also char*.
You don't give value to a pointer to char this way:
*a = {"hello"};
You have to use:
a="hello";
I don't understand very well what you are trying to do. If you only want to print "hello" in your screen, why do you use a pointer to char? What is the getch() for? You use that function this way: http://linux.die.net/man/3/getch Do you intend to read a character?
I would only do:
#include <stdio.h>
main()
{
printf("hello");
}
What are you exactly trying to do?
This is a good reference guide: http://www.gnu.org/software/gnu-c-manual/gnu-c-manual.html
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
void main()
{
char *a;// a is pointer which address is store in stack and when u initialize with any string then string is store in code section which cant be change
clrscr();
a = "hello";// this is the way to store the string but if when u assign while declaring as char *a= hello this will accept Remember hello is store in code where as address of pointer a is store in stack section
printf("%s", a);
getch();
}
~
just providing another insight for you..
just now I tried this in Dev-C++ 5.6.3 and it works..
if you assign the value directly when you are declaring it, it works:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
int main()
{
system("cls");
char *a = {"hello"};
printf("%s",a);
getch();
return 0;
}
and one more thing, clrscr is not a standard c function (it didn't work in my test), so how about using cls in stdlib like I did.. hope it's useful..