48
votes
Accepted
Difference between char and char* in c
The difference between char* the pointer and char[] the array is how you interact with them after you create them.
If you are just printing the two examples, it will perform exactly the same. They ...
- 759
7
votes
Difference between char and char* in c
C99 N1256 draft
There are two different uses of character string literals:
Initialize char[]:
char c[] = "abc";
This is "more magic", and described at 6.7.8/14 "Initialization":
An array ...
7
votes
Accepted
Pointers and arrays, don't understand the relationship
OK, this is going to be long answer, because what you've asked spans many subjects, so bare with me.
Arrays vs Pointers
First, let's learn the difference between arrays and pointers. When you ...
- 7,366
6
votes
Accepted
PSET2: CRACK vs POINTERS
Your problem is that letters[i] is a char, but the crypt() function expects a char * (string) as its first argument. So what you really want to do is pass a string with a single character, not a ...
- 7,366
4
votes
How can char* contain a collection of characters instead of a memory address?
what's going on under the hood?
when having
char *s = "hello";
basically an array of chars is created for you and is initialized with the contents of the string literal (in this case, "hello"). The ...
- 17.6k
3
votes
Accepted
Pset4 - Recover (usage of sprintf)
Before I answer this question and important clarification must be made about what it means for something to "work". There is a term that will come up a lot when talking about C (and other languages) ...
- 239
2
votes
Accepted
Want to make sure I understand struct, malloc, and pointers and how they relate to each other
I originally didn't alloc for the struct, just declared it. It worked,
and from what I understand, it worked because it was created in the
stack? Am I correct?
the type BITMAPINFOHEADER is not a ...
- 17.6k
2
votes
Accepted
Whats the adress of this string?
You can either do:
char* str = "world";
In this case, world automatically will be stored in some chunk of memory and the adress of the first byte of this chunk will be stored in str. Using this ...
- 1,389
2
votes
3 days failing to get bubblesort working .. devastated :(
why do you use int* array[n]; and not int array[n];?
you aren't calling your sort() function.
- 425
2
votes
Accepted
Why does "if (input[i] == " ")" throw an error of "incompatible integer to pointer conversion"?
It turns out that using double quotes vs single quotes makes a difference in C!
Double quotes are used to wrap a STRING, while single quotes are used to wrap a CHARACTER. This is easy to remember if ...
- 253
2
votes
Why does this snippet of code work?
From the inside out:
(arr + i) is a pointer to the i'th element of arr
*(arr + i) is the contents of the pointer to i'th element of arr (dereferenced (arr + i))
&(*(arr + 1) is the address of ...
- 27.5k
2
votes
Accepted
What happens when you assign a character string to a char* instead of writing element by element?
I'll let you visualize this for yourself. Go here http://www.pythontutor.com/c.html#mode=edit and paste your code, then hit "visualize execution".
You'll see graphically what's happening in memory.
...
- 2,595
2
votes
Accepted
I have written this swap code but it is not compiling, Where's the bug
You don't need to specify the return type of your function when you call it, only when you declare/define it.
So you've declared the function right, like this: void swap (int* a,int* b)
But when you ...
- 2,595
2
votes
Accepted
pset4 resize file header question
*= and += (same for many other operators) are combined assignment operators. a += b is like a = a + b, a *= b is like a = a * b, and so on.
In the line newbi.biWidth = bi.biWidth *= n; you also use ...
- 20.8k
2
votes
Accepted
C: Vigenere - incompatible pointer types passing 'char **' to parameter of type 'const char *
The error comes from here:
for(int l=0; l<strlen(argv); l++)
Think about it. strlen() wants a const char* or char*. You gave it argv, which is an array of the command line arguments. Because argv ...
- 800
2
votes
Error with comparison between pointer and integer
What is ! = 2? Does not look like a valid expression to me.
Use if (argc != 2).
Instead of the other ! = 0, something like if (strlen(plaintext) != 0) would be correct, this condition would always ...
- 20.8k
2
votes
Accepted
Difference between s and *s in pointer
printf("%c\n", *s); is the same as
printf("%c\n", s[0]);
By definition of printf, the %c format specifier in printf expects a character.
So, *s says go to the address stored in s, and give me the ...
- 18.5k
2
votes
pointers and scanf : when do I use &x, *x or just x?
I think you got it. scanf needs a memory address so it knows where to write to.
Whenever you add & you get the memory address of what follows. But in case of strings, what we pass as a string ...
- 20.8k
2
votes
Accepted
recover pset4 file handling
1 It's not a single char. You've declared an array of 8 chars, which is what you need to hold "000.jpg"
{'0', '0', '0', '.', 'j', 'p', 'g', '\0'}
2 bookmark is an array. An array will "decay" to a ...
- 18.5k
2
votes
Accepted
Using * and & both works
buffer is an array. Array variables "decay" into pointers when needed, so fwrite will understand buffer to mean a pointer to the first element (ie, the same as &buffer).
- 18.5k
2
votes
Accepted
Printf doesn't print whole string "11111"
*a is same as a[0], pointing to the first element of a. Generally, *(a + i) is same as a[i].
I don't think you want an array of strings (string is a typedef of char*) all pointing to the same ...
- 20.8k
2
votes
Accepted
vigenere output "incompatible pointer types"
string out[n];
You've declared an array of strings with this declaration. What you probably want is one string (ie, an array of chars). Change that to char out[n]; to get one string.
- 18.5k
2
votes
Accepted
Pointer passed as argument, then suddenly goes null
Look at exactly what this code does:
void setCharacter(char* pCharacter)
{
pCharacter = malloc(sizeof(char));
*pCharacter = 'b';
}
Remember that things get passed by copy. First, ...
- 66.7k
2
votes
Accepted
I know that my recover is wrong, but I'm not sure what steps to take-
There are several issues and areas for improvement here.
First, there's this:
FILE *f = fopen("argv[1]", "r");
By putting argv[1] in quotes, you're saying to treat whatever is in ...
- 66.7k
2
votes
Accepted
incompatible pointer types error
create_family is the name of your function. In order to call it, be sure you don't forget the ( ) with the argument.
If I wanted to create a family with generations number of generations, then I'd ...
- 18.5k
2
votes
Accepted
PSET4 Blur not passing Check50
Without doing a deep dive, I do see a major problem in the logic. The code copies all the pixels from image to temp array. But then, it does all the calculations using the temp array as the source ...
- 66.7k
1
vote
Accepted
I made this copy program as in the shorts by david but it is giving two error described below
char* t = malloc(strlen(s)+1) * sizeof(char);
this is wrong, we can not multiply a pointer with an integer, as the error says multiplication operators are not compatible,the correct way would be
...
- 5,201
1
vote
I made this copy program as in the shorts by david but it is giving two error described below
malloc() is declared in <stdlib.h>, so you must include that. I believe adding the stdlib header will fix your second error as well, since t[0] doesn't actually exist as the call to malloc() ...
- 1,223
1
vote
Accepted
Pset5: check() failed and questions about the usage of pointer
There are a few issues to be dealt with. First, in load(), when a node is created with a call to malloc, it is not then initialized, so it contains garbage data, not NULLs in all of the addresses. ...
- 66.7k
1
vote
Accepted
A question about the fread function
Simply put, the pointer will automatically be moved to point at the first unread byte past whatever was read in that particular statement, whether it was 1 byte or 512 bytes, or anything else. If you ...
- 66.7k
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
pointers × 144pset4 × 20
pset5 × 20
c × 19
speller × 13
array × 12
recover × 11
string × 9
malloc × 9
cs50x × 7
pset6 × 7
pset2 × 6
load × 6
valgrind × 5
memory-leak × 5
memory × 5
linked-list × 5
structs × 5
resize × 4
vigenere × 4
unload × 4
hash-table × 4
arrays × 4
char × 4
file-io × 4