8
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 ...
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 ...
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) ...
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
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 ...
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 ...
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 ...
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
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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).
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 ...
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.
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, ...
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 ...
2
votes
Accepted
PSET2 Caesar - Stuck at 'isdigit' step: Do I need to learn about pointers, or is something else going on?
Its a simple and common issue with new programmers. ;-)
Yes, argv[] is an array of strings. So, argv[1] is a string. Now, say that argv[1] contains the string "cat". Then argv[1][0] ...
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 ...
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 ...
1
vote
Accepted
pointer for function using global variable
Interesting question. The answer is that a global variable does not work in your program.
The compiler warning is really telling you that the variable a, in main, is different from the variable a in ...
1
vote
Accepted
Week 5 - Speller. Instance remaining as NULL after setting inside function
Function parameters in C are usually passed by value, creating a variable that's initialised to the passed value. That means your assignment to tree changes the variable tree in addWordToTree, but ...
1
vote
Accepted
incompatible pointer to integer conversion
The problem is your declaration of name as an int. get_string tries to return a string (or char array). It can't store the string ( a char array )in an int. The second error is similar. Since name ...
1
vote
Accepted
PSET6 - load function - 1 file is not loaded (the jpg)
The primary point of failure is here memcpy(*content, buffer, ret);. You are always copying buffer into the first byte of content. Since most of the test files are less than 512 bytes.....
This ...
1
vote
the pointer arrays
It really is pointer arithmetic. Try printf("%c\n",*(msg+6)); to see the difference. You'd have to use printf because the argument to puts is a const char*, and the result of *(msg + 6) is a char.
*(...
1
vote
Accepted
What does a command line argument stored in char* argv[] yields when assigning to another char*???
argv[1] and argv[2] isn't assigned inf and outf, rather inf and outf is assigned to them. inf and outf both hold pointers to the first char in the string literal they point to, and when printed ...
1
vote
Pointer variable
On 32 bit systems such as IDE50 memory addresses (pointers) are 32 bits as the name would suggest; likewise on 64 bit systems the memory addresses are 64 bits.
32 / 8 = 4
64 / 8 = 8
So a pointer is ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
pointers × 141pset5 × 20
pset4 × 19
c × 18
speller × 13
array × 12
recover × 11
string × 9
malloc × 9
pset6 × 7
cs50x × 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